Skip to content Skip to sidebar Skip to footer

Word Boundary Won't Match The Beginning Or End In Javascript

I'm getting unexpected results with this code: 'foo'.match(new RegExp('\bfoo\b')); // Returns null Why is this returning null while this one returns 'foo'? 'foo'.match(new RegExp(

Solution 1:

Escape the backslashes

'foo'.match(newRegExp('\\bfoo\\b'));

Solution 2:

Don't wrap it in quotes... instead, do this:-

'foo'.match(newRegExp(/\bfoo\b/))

Post a Comment for "Word Boundary Won't Match The Beginning Or End In Javascript"