javascript - regex returning un expected value -
this question has answer here:
- javascript regex not working 1 answer
i expected new regexp('\b\w{1,7}\b', "i").test('bc4rg6')
return true since want test of string "bc4rg6" alphanumeric , has 1 7 characters. browser giving false. how fix can test condition stated? thanks
you need escape backslashes in string, because \b
escape sequence turns backspace character.
console.log(new regexp('\\b\\w{1,7}\\b', "i").test('bc4rg6'));
but if regexp constant, don't need use new regexp
, use regexp literal.
console.log(/\b\w{1,7}\b/i.test('bc4rg6'))
Comments
Post a Comment