JavaScript (ES2026) reference · String Methods

JavaScript String match() Method

Match the string against a regular expression and return the matches (all of them with the g flag) or null.

JavaScript (ES2026) Since ES3

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.match(regexp)

Parameters

ParameterDescription
regexp A regular expression (a string is converted to one).

Return value

An array of matches, or null.

Examples

All matches vs the first with groupsTry it yourself
const text = "Order 42 shipped, order 7 pending";
console.log(text.match(/\d+/g));
console.log(text.match(/order (\d+)/i)[1]);

Output

[ '42', '7' ]
42
No match returns nullTry it yourself
const m = "nothing here".match(/\d+/g);
console.log(m, m ?? []);

Output

null []

Version history

VersionChange
JavaScript ES3Added

Was this page helpful?