match()

ES3+

Retrieves the result of matching a string against a regular expression.

Syntax

string.match(regexp)

Parameters

regexp RegExp

A regular expression object

Return Value

Array | null

An Array of matches, or null if no match was found

Examples

JavaScript
const str = 'The rain in Spain';
console.log(str.match(/ain/g));
console.log(str.match(/xyz/));
Output:
// ['ain', 'ain'] null

Related Methods