tags:

views:

48

answers:

2

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

+3  A: 

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 .maatch() (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.

Nick Craver
A: 

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