Unsere Freiwilligen haben diesen Artikel noch nicht in Deutsch übersetzt. Machen Sie mit und helfen Sie, das zu erledigen!
Sie können den Artikel auch auf English (US) lesen.

The matchAll() method returns an iterator of all results matching a string against a regular expression, including capturing groups.

Syntax

str.matchAll(regexp)

Parameters

regexp
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

Return value

An iterator (which is not a restartable iterable).

Examples

Regexp.exec() and matchAll()

Prior to the addition of matchAll to JavaScript, it was possible to use calls to regexp.exec (and regexes with the /g flag) in a loop to obtain all the matches:

const regexp = RegExp('foo*','g');
const str = 'table football, foosball';

while ((matches = regexp.exec(str)) !== null) {
  console.log(`Found ${matches[0]}. Next starts at ${regexp.lastIndex}.`);
  // expected output: "Found foo. Next starts at 9."
  // expected output: "Found foo. Next starts at 19."
}

With matchAll available, you can avoid the while loop and exec with /g.
Instead, by using matchAll, you get back an iterator which you can use with the more convenient for...of, array spread, or Array.from() constructs:

const regexp = RegExp('foo*','g'); 
const str = 'table football, foosball';
let matches = str.matchAll(regexp);

for (const match of matches) {
  console.log(match);
}
// Array [ "foo" ]
// Array [ "foo" ]

// matches iterator is exhausted after the for..of iteration
// Call matchAll again to create a new iterator
matches = str.matchAll(regexp);

Array.from(matches, m => m[0]);
// Array [ "foo", "foo" ]

Better access to capturing groups

Another compelling reason for matchAll is the improved access to capture groups. Capture groups are ignored when using match() with the global /g flag:

var regexp = /t(e)(st(\d?))/g;
var str = 'test1test2';

str.match(regexp); 
// Array ['test1', 'test2']

With matchAll you can access them:

let array = [...str.matchAll(regexp)];

array[0];
// ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', length: 4]
array[1];
// ['test2', 'e', 'st2', '2', index: 5, input: 'test1test2', length: 4]

Specifications

Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'String.prototype.matchAll' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
matchAllChrome Full support 73Edge No support NoFirefox Full support 67IE No support NoOpera Full support 60Safari No support NoWebView Android Full support 73Chrome Android Full support 73Firefox Android Full support 67Opera Android Full support YesSafari iOS No support NoSamsung Internet Android Full support Yesnodejs Full support 12.0.0

Legend

Full support  
Full support
No support  
No support

See also

Schlagwörter des Dokuments und Mitwirkende

Mitwirkende an dieser Seite: sideshowbarker, SaschaNaz, fscholz, mdnwebdocs-bot
Zuletzt aktualisiert von: sideshowbarker,