Search code examples
javascriptnode.jsredex

Error in Regex101 or my code? JavaScript / Node


I am currently doing an exercise that I got from my school. It's about regex but I am not sure if there is something wrong with my function or my regex code. We were told to use regex101.com to try things out.

in that site it looks like this and it all seems to work.

code in regex101

But in my file I get this. my own code

Here is the code:

function isCheck(words) {
  const check = /\Bche(ck|que)/;
  return check.test('check', 'cheque');
}

So I am thinking that maybe there is something wrong in my function but I am not sure what that could be.

this is what its testing against

describe('The check-checker', () => {
  it.only('should match check', () => {
    const check = 'check';
    assert.equal(matcher.isCheck(check), true);
  });

  it.only('should match cheque', () => {
    const cheque = 'cheque';
    assert.equal(matcher.isCheck(cheque), true);
  });

Does anyone have any ideas?


Solution

  • I finally found it, for anyone else in a similar situation. The function was wrong. I needed a parameter and to call on it in the function.

    function isCheck(test) {
      const check2 = /\b(check|cheque)\b/i;
      if (check2.test(test)) {
        return true;
      }
      return false;
    }
    module.exports.isCheck = isCheck;
    

    There is the code.