Symbol.matchAll

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

Descrição

Esse símbolo é usado pelo String.prototype.matchAll() e especificado no RegExp.prototype[@@matchAll]() (en-US). 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
Writable no
Enumerable no
Configurable no

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]() (en-US) para mais exemplos.

Especificações

Specification
ECMAScript Language Specification
# sec-symbol.matchall

Compatibilidade com navegadores

BCD tables only load in the browser

Veja também