String.prototype.match()

Matches a string against a regular expression and returns the result.

Since ES1 Spec ↗

Syntax

str.match(regexp)

Parameters

NameTypeRequiredDescription
regexp RegExp Yes A regular expression. A non-RegExp value is converted via `new RegExp()`.

Returns

Array | null — Without the g flag, an array with match details and groups; with g, an array of all matches; null if no match.

Examples

console.log('a1b2c3'.match(/\d/g));
Output
[ '1', '2', '3' ]
const m = 'name: Ann'.match(/name: (\w+)/);
console.log(m[1]);
Output
Ann

Notes

Returns null (not an empty array) when there is no match - guard before indexing. With the g flag, capture groups are NOT included. For groups across all matches use `matchAll`.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also