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:
verify(/\b[^e]+\b/i,
["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.