regex.test V.S. string.match to know if a string matches a regular expression

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


Is there any difference between this:

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

They seem to give the same result?

How to generate string matches regular expression in .NET [closed]

Can you tell me please how to generate string that matches particular regular expression using .NET 4? Thanks!

Regular expression that matches number in string but not percentages

I need to know if there is a regular expression for testing for the presence of numbers in strings that: Matches Lorem 20 Ipsum Matches Lorem 2,5 Ipsum Matches Lorem 20.5 Ipsum Does not match Lorem 2

Javascript regular expression that matches two strings

I don’t understand that much regular expression and I want to create one that matches two strings. I know that for one string the code is .match(/something/) and if matches on of two strings it is .ma

Regular expression which matches regular expressions

Is it possible to write a regular expression which matches regular expressions? Does anyone have examples? If there is some theoretical obstruction, does anyone know of a regex which will match at lea

How to find out if a substring of a Java string matches a regular expression?

The Java String class has a matches(String regex) method that checks whether the current string matches the given regular expression. However, how can we find whether a substring of the current string

Regular expression that matches 3 characters anywhere in the string

I need a regular expression that matches for example ap**3 or 1*2*3 or 12**r. So far i have this regular expression: /[a-z0-9]{3}/g, but this only matches 3 characters that are next to one another. Is

Regular expression to get all matches in string ends with [closed]

Please help me to develop a regular expression which will return all matches in a string if its end with (&any character) Eg. String a=abcd(&w), then matches should have value (&w) If a=

Why this string matches the regular expression?

Why this string matches the pattern ? pattern = ^Page /d of /d$| ^Group /d Notes$| ^More word lists and tips at$| re.match(pattern, stackoverflow, re.VER

How to check if string matches a regular expression in objective-c?

since regular exressions are not supported in Cocoa I find RegexKitLite very usefull. But all examples extract matching strings. I just want to test if a string matches a regular expression and get a

How to generate a regular expression that matches one specific string

Here, I’ve tried to create a regular expression that matches one particular string: #This is the string that the regex is intended to match theString = ..!-+!)|(!+-!.. print(re.compile(theString).ma


First, what each function does:

test: a regex function

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

match: a string function

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

Since null == false if the string doesn’t have a match the Boolean value will be false.

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 is significant?
The answer once more is YES! jsPerf I made shows the difference is ~30% – ~60% depends on the browser:

regex.test V.S. string.match to know if a string matches a regular expression

So, to conclude the answer, if you only want a boolean answer use .test instead of .match

Amazed by not finding even a single blog or a Stack Overflow answer entry to my question when searched for “regex test vs match javascript”, made me use the share your knowledge, Q&A-style feature. Hope you’ll like it.

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.