String.prototype.match()

text.match(regexp)

Matches the string against a regular expression. Without the g flag it returns the first match with its groups and position, with the g flag an array of all matched strings. With no match it returns null.

Parameters

  • regexp – a regular expression (a string is converted to an expression).

Return value

A match array (with the index, input, groups properties), an array of all matches, or null.

Examples

const message = 'Order 1024 placed on 3/15/2026';
console.log(message.match(/\d{4}/));
console.log(message.match(/\d+/g));
console.log(message.match(/(?<month>\d+)\/(?<day>\d+)\/(?<year>\d+)/).groups.year);
console.log('no digits'.match(/\d+/));

Output:

[ '1024', index: 6, input: 'Order 1024 placed on 3/15/2026', groups: undefined ]
[ '1024', '3', '15', '2026' ]
2026
null

Things to watch out for

  • With no match it returns null, not an empty array – text.match(/x/g).length then ends with an error. Write (text.match(/x/g) || []).length.
  • With the g flag it does not return groups; matches with groups are returned by matchAll().
  • A plain “matches / does not match” test is faster with regexp.test(text).

Browser support

Baseline – widely available: works in all major browsers and has for a long time (since 2015 at the latest). Since version: Chrome 1, Firefox 1, Safari 1; Node.js 0.10.0. As of October 2026 (MDN browser-compat-data).

Related:
String.prototype.matchAll()
String.prototype.search()
String.prototype.replace()
RegExp
RegExp.prototype.exec()