简体   繁体   中英

what is opposite to javascript match()

if i want to match some thing with 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() , eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM