Symbol.matchAll

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

O símbolo Symbol.matchAll é conhecido por retornar um iterador, que produz conrrespondências de uma expressão regular com uma string. Essa função é usada pelo método String.prototype.matchAll().

Experimente

const re = /[0-9]+/g;
const str = "2016-01-02|2019-03-07";
const result = re[Symbol.matchAll](str);

console.log(Array.from(result, (x) => x[0]));
// Expected output: Array ["2016", "01", "02", "2019", "03", "07"]

Descrição

Esse símbolo é usado pelo String.prototype.matchAll() e especificado no RegExp.prototype[@@matchAll](). Os próximos dois exemplos retornam o mesmo resultado:

js
"abc".matchAll(/a/);

/a/[Symbol.matchAll]("abc");

Esse método existe para costumizar o comportamento conrrespondente com as subclasses RegExp.

Property attributes of Symbol.matchAll
Writableno
Enumerableno
Configurableno

Exemplos

Usuando Symbol.matchAll

js
let re = /[0-9]+/g;
let str = "2016-01-02|2019-03-07";

const numbers = {
  *[Symbol.matchAll](str) {
    for (const n of str.matchAll(/[0-9]+/g)) yield n[0];
  },
};

console.log(Array.from(str.matchAll(numbers)));
//  Array ["2016", "01", "02", "2019", "03", "07"]

Veja String.prototype.matchAll() e RegExp.prototype[@@matchAll]() para mais exemplos.

Especificações

Specification
ECMAScript® 2025 Language Specification
# sec-symbol.matchall

Compatibilidade com navegadores

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
matchAll

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Veja também