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 janeiro de 2020.
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 | |
|---|---|
| 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]() para mais exemplos.
Especificações
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-symbol.matchall> |