开发者

what is opposite to javascript match()

开发者 https://www.devze.com 2023-01-17 10:10 出处:网络
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:

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 returns null.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消