JavaScript String match() Method

Definition and Usage

match() The method matches the string with the regular expression.

Tip:If the search value is a string, it is converted to a regular expression.

match() The method returns an array containing the matches.

If no match is found, then match() The method returns null.

See also:

Regular Expression Tutorial

Regular Expression Reference Manual

The difference between String match() and String search()

match() The method returns the match array.

search() The method returns the position of the first match.

Instance

Example 1

Search for "ain" using string:

let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");

Try it yourself

Example 2

Search for "ain" using regular expression:

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);

Try it yourself

Example 3

Global search for "ain":

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);

Try it yourself

Example 4

Global, case-insensitive search:

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);

Try it yourself

Syntax

string.match(regexp)

Parameter

Parameter Description
regexp

Required. Search value.

Regular expression (or a string that will be converted into a regular expression).

Return value

Type Description
Array or null

An array containing the matches.

If no match is found, it returns null.

Technical details

Parameter regexp

This parameter specifies the RegExp object to be matched. If this parameter is not a RegExp object, it will be passed to the RegExp() constructor first and converted into a RegExp object.

Return value

The array that stores matching results. The content of this array depends on regexp Whether it has the global flag g. The following explains this return value in detail.

Explain

match() method will search the string stringTo find one or more regexp Matching text. The behavior of this method is largely dependent on regexp Whether it has the flag g.

If regexp without the flag g, then match() method can only string Once. If no matching text is found,match() Will return null. Otherwise, it will return an array containing information related to the matching text it finds. The first element of the array contains the matching text, and the other elements contain the text that matches the subexpressions of the regular expression. In addition to these conventional array elements, the returned array also contains two object properties. The index attribute declares the starting character position of the matching text in string Position, the input attribute declares the position of string reference.

If regexp flag g, then match() method will perform a global search, finding string All matching substrings. If no matching substrings are found, then null. If one or more matching substrings are found, an array is returned. However, the content of the array returned by global matching is quite different from the former, its array elements contain string All matching substrings, and also do not have the index attribute or input attribute.

Note:In global search mode,match() Which does not provide information about the text that matches the subexpression, nor does it declare the position of each matching substring. If you need this global search information, you can use RegExp.exec().

Regular expression search methods

In JavaScript, regular expression text search can be completed with different methods.

These are the most commonly used methods by using the pattern as a regular expression:

Example Description
text.match(pattern) String method match()
text.search(pattern) String method search()
pattern.exec(text) Regular expression method exec()
pattern.test(text) Regular expression method test()

Browser support

match() Is ECMAScript1 (ES1) feature.

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support

Related pages

JavaScript String

JavaScript String Methods

JavaScript String Search