Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is opposite to javascript match()

Tags:

javascript

if i want to match some thing with javascript i can use foo.match(); but how can i check if it not match...

like image 407
Web Worm Avatar asked May 02 '26 05:05

Web Worm


2 Answers

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.

like image 110
Nick Craver Avatar answered May 03 '26 18:05

Nick Craver


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);
like image 26
Guffa Avatar answered May 03 '26 20:05

Guffa