Favorites

JavaScript String.prototype.matchAll() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
String.prototype.matchAll()
JavaScript String Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

The matchAll() method returns an iterator of every regular expression match in a string, including capture groups for each one.

Description

The matchAll() method returns an iterator of all matches, where each match is an array including its capture groups, like a non-global match() result. The regular expression must have the g flag, or a TypeError is thrown.

Each match in the iterator has the same shape as a non-global match() result: the full match, the capture groups, index and groups for named groups. Because it's an iterator, you can loop over it with for...of, spread it into an array with [...str.matchAll(re)], or use Array.from() to map the matches in one step.

Unlike looping with exec(), matchAll() works on a copy of the regular expression, so it doesn't change the original's lastIndex, and there's no risk of an infinite loop from a pattern that matches an empty string.

Syntax

string.matchAll(regexp)

Parameters

  • regexpA regular expression with the g flag.

Return Value

An iterator of match arrays, each with capture groups, index and groups.

Exceptions

  • TypeErrorThrown if the regular expression doesn't have the g flag.

Example

const text = 'x=1, y=22, z=333';
for (const m of text.matchAll(/(\w)=(\d+)/g)) {
  console.log(m[1], Number(m[2]), 'at', m.index);
}

Browser Support

The following information will show you the current browser support for the String.prototype.matchAll() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee