-1

I am studying Eloquent JavaScript chapter 9 and one of the problem is to find a word without e.

The answer contains a [^\We], but I don't see why the \W is important there. Sure together the square bracket means anything in the set of word character and not letter e, but why can't I just omit the \W? I tried and it didn't work.

I had

/.*\b[^e]+\b.*/

which simply return everything. The correct answer is

/\b[^\We]+\b/i

.

j08691
  • 204,283
  • 31
  • 260
  • 272
Zack
  • 13
  • 4

1 Answers1

3

\W is a non-word character. See documentation.

Your regular expression is [^\We]. This will match the negation of non-word characters and the letter 'e'. In other words, it matches word characters except the letter 'e'.

You can't omit the \W because without it, your regexp will match everything except 'e', which isn't the requirement (match the set of word characters except 'e').