Can anybody please tell me why doesn’t this Regexp match?
var matches = ' @test'.match(new RegExp('(\s+|^)(@|!)(.*?)(\s+|$)', 'g'));
whereas this one matches:
var matches = '@test'.match(new RegExp('(\s+|^)(@|!)(.*?)(\s+|$)', 'g'));
I have already specified \s+. Why wouldn’t it match it then?
\sis not recognized as the whitespace shorthand because you’re constructing the regex from a string instead of a regex literal, and inside a string you need to double the backslashes.So you need to use
or