Javascript RegExp Question

I have this function:

function verify(regexp, yes, no) {
  // Ignore unfinished exercises
  if (regexp.source == "...") return;
  for (let str of yes) if (!regexp.test(str)) {
    console.log(`Failure to match '${str}'`);
  for (let str of no) if (regexp.test(str)) {
    console.log(`Unexpected match for '${str}'`);

and this call to it:

       ["red platypus", "wobbling nest"],
       ["earth bed", "learning ape", "BEET"]);

which is intended to match a word without the letter e or E, but it matches on the strings “earth bed” and “learning ape” when it shouldn’t.

Calling the function with the regexp /\b[^\We]+\b/i doesn’t match on those strings (and has been presented to me as a correct solution), but I’m not entirely sure why.

I can see why the expression including \W is the better one as it excludes non-word characters but I don’t see why my expression is matching those two strings, it seems like it should be excluding both based on the fact that there is no bounded word that doesn’t contain an e in either string.

It seems to have to do with the space as “BEET” doesn’t match but “BEET BEET” does and eliminating the space stops it from matching the other strings. Clearly the \W would deal with a space but I’m not sure why it’s a problem given that there are also e’s.

It matches the space in between the words. Which kind of makes sense, as both before and after the space there’s the word boundary.

const match = 'earth bed'.match(/\b[^e]+\b/i)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.