388

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?

3
  • 4
    these are the best tests you will find jsperf.com/regexp-test-vs-match-m5
    – ajax333221
    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.
    – gdoron
    Commented Jun 8, 2012 at 7:28
  • "Does a string match a presented pattern?" vs "Does my pattern match a presented string?". str.match(/{regex}/) seems more logical to read than /{regex}/.test(str). Maybe next-gen javascript should include a str.like(/{regex}/) that behaves like test but reads like match. Commented Nov 20, 2022 at 23:23

2 Answers 2

584

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.

12
  • 9
    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(). :)
    – user1106925
    Commented Jun 7, 2012 at 22:30
  • 38
    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. Commented Nov 14, 2013 at 21:12
  • 2
    Interestingly test is 41% slower than match for me using the jsPerf test above (Chrome 41, OSX).
    – Benjie
    Commented Apr 13, 2015 at 8:31
  • 1
    @AlexShilman indexOf is faster (but not much) than test according to this stackoverflow.com/questions/183496/… (you'd expect it to be faster).
    – podperson
    Commented Jul 21, 2016 at 20:51
  • 11
    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.
    – davertron
    Commented Jul 19, 2019 at 14:19
202

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
  • 39
    I was just head banging seeing that my regex.test() was randomly logging "true" then "false" then "true"...thanks! Commented Apr 11, 2014 at 15:29
  • 19
    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.
    – James
    Commented Sep 30, 2015 at 15:35
  • 9
    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/…
    – Dan
    Commented Nov 10, 2016 at 15:47
  • 4
    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.
    – Nateowami
    Commented Mar 19, 2017 at 7:10
  • 4
    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?
    – buhbang
    Commented Aug 20, 2018 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.