JavaScript String.prototype.match() Method
The match() method searches a string with a regular expression and returns the matches, or null if there are none.
Description
The match() method retrieves the result of matching the string against a regular expression. Without the g flag, it returns the first match along with its capture groups, named groups and position. With g, it returns an array of every matching substring, but no groups.
It returns null when nothing matches, so check before using the result. To get capture groups for every match, use matchAll().
A string argument is converted to a regular expression, so special characters such as . and ? keep their regex meaning; escape them, or use includes() for plain text. The index property of a non-global result tells you where the match starts. When you only need a yes or no, RegExp.prototype.test() is clearer and slightly faster.
Syntax
string.match(regexp)Parameters
- regexpA regular expression (a string is converted to one).
Return Value
Without the g flag, an array with the full match and capture groups (plus index and groups), or null. With g, an array of all matches, or null.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.match() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





