if i want to match some thing with开发者_StackOverflow社区 javascript i can use foo.match(); but how can i check if it not match...
To be more explicit, I tend to use !
and .test()
, e.g:
var hasNoMatch = !/myregex/.test(string);
Since since by the spec .match()
returns null in the case of no matches, this works as well:
var hasNoMatch = !foo.match();
From the MDC documentation for .match()
(much quicker resource most of the time:
If the regular expression includes the g flag, the method returns an
Array
containing all matches. If there were no matches, the method returnsnull
.
If you are just testing if a pattern matches, you should use the test
method as Nick suggested.
If you want to find something that doesn't match the pattern, you can change the pattern to match everything except that. For example using a negative set:
// find uppercase characters
var m = s.match(/[A-Z]+/g);
// find everything except uppercase characters
var m = s.match(/[^A-Z]+/g);
精彩评论