397

Many times I'm using the string match function to know if a string matches a regular expression.

if(str.match(/{regex}/)) 

Is there any difference between this:

if (/{regex}/.test(str)) 

They seem to give the same result?

2
  • 4
    these are the best tests you will find jsperf.com/regexp-test-vs-match-m5 Commented Jun 7, 2012 at 22:13
  • @ajax333221. Thanks for the jsperf, but I'm not sure it's a good one. The regex match using a match group, which isn't needed when looking a boolean value. Commented Jun 8, 2012 at 7:28

2 Answers 2

588

Basic Usage

First, let's see what each function does:

regexObject.test( String )

Executes the search for a match between a regular expression and a specified string. Returns true or false.

string.match( RegExp )

Used to retrieve the matches when matching a string against a regular expression. Returns an array with the matches or null if there are none.

Since null evaluates to false,

if ( string.match(regex) ) { // There was a match. } else { // No match. } 

Performance

Is there any difference regarding performance?

Yes. I found this short note in the MDN site:

If you need to know if a string matches a regular expression regexp, use regexp.test(string).

Is the difference significant?

The answer once more is YES! This jsPerf I put together shows the difference is ~30% - ~60% depending on the browser:

test vs match | Performance Test

Conclusion

Use .test if you want a faster boolean check. Use .match to retrieve all matches when using the g global flag.

Sign up to request clarification or add additional context in comments.

12 Comments

Not too surprised since the string function needs to flip things around and then create the Array if there's a match. Looks like I'll keep using .test(). :)
My two cents: performance is overrated. Either option can do ~15,000 operations in the flicker of a monitor, so unless you're doing bulk regex client-side, speed isn't relevant. Of course 'test' is logically the correct function if a boolean result is what you're after. Thanks for the Q/A BTW.
Interestingly test is 41% slower than match for me using the jsPerf test above (Chrome 41, OSX).
@AlexShilman indexOf is faster (but not much) than test according to this stackoverflow.com/questions/183496/… (you'd expect it to be faster).
One thing that might bite you here (it bit my team recently): If you use the 'g' flag on your Regex and create a new instance (i.e. via new RegExp(<regex_str>, 'g')) and you reuse that instance, running "test" is stateful, i.e. will return different results when run multiple times. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… for details.
|
207

Don't forget to take into consideration the global flag in your regexp :

var reg = /abc/g; !!'abcdefghi'.match(reg); // => true !!'abcdefghi'.match(reg); // => true reg.test('abcdefghi'); // => true reg.test('abcdefghi'); // => false <= 

This is because Regexp keeps track of the lastIndex when a new match is found.

7 Comments

I was just head banging seeing that my regex.test() was randomly logging "true" then "false" then "true"...thanks!
I think this is the better answer. It explains that they don't give the same result and that reg.test() has a dangerous pitfall. To me this makes string.match() the clear choice. Performance has never been any issue for me.
This is important! Going crazy trying to figure out why every other result was missing...for reference of anyone else that finds this: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
If you're as confused as I was, see stackoverflow.com/q/1520800/3714913. There's also String.prototype.search(), which returns an index but doesn't have this issue as far as I can tell.
Just curious, what's the point of having a global flag for .test()? isn't the point of .test() to check if the string has a matching regexp?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.