javascript - * sign in regexp forces to match a letter -


i want match word , 0 or more letters after it. have constructed following regexp: /test\w*/
\w letter, * 0 or more match.
works incorrectly. matches testing tests, not test itself. here code using testing:

console.log(new regexp("test" + "[\\w*]").test("tests")); // true console.log(new regexp("test" + "[\\w*]").test("test")); // false - should true console.log(new regexp("test" + "[\\w*]").test("test-")); // false console.log(new regexp("test" + "[\\w*]").test("testing")); // true console.log(new regexp("test" + "[\\w*]").test("test@")); // false 

update expression works wanted in regexr.com

the * inside brackets doesn't means 0 or more, literally char * matched. edited: if not want match words "test-", use end of string anchor $ this:

console.log(new regexp("test" + "\\w*$").test("test"));  

explanation on why regex fails:

console.log(new regexp("test" + "[\\w*]").test("test"));  

means: match word test, char \w or char *, that's why other cases match , "test" doesn't matches, regex match "test*" instance.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -