javascript - regex returning un expected value -


this question has answer here:

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

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -