RegExp.prototype[Symbol.matchAll]()

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流浏览器均已支持。

[Symbol.matchAll]() 方法在 RegExp 实例上,用于指定 String.prototype.matchAll 的行为。

试一试

class MyRegExp extends RegExp {
  [Symbol.matchAll](str) {
    const result = RegExp.prototype[Symbol.matchAll].call(this, str);
    if (!result) {
      return null;
    }
    return Array.from(result);
  }
}

const re = new MyRegExp("-\\d+", "g");
console.log("2016-01-02|2019-03-07".matchAll(re));
// Expected output: Array [Array ["-01"], Array ["-02"], Array ["-03"], Array ["-07"]]

语法

js
regexp[Symbol.matchAll](str)

参数

str

一个作为匹配目标的 String

返回值

一个匹配项的 可迭代迭代器对象(不可重置)。每个匹配项都是一个数组,其形状与 RegExp.prototype.exec() 的返回值相同。

描述

此方法在内部由 String.prototype.matchAll() 调用。例如,以下两个示例返回相同的结果。

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

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

[Symbol.split]() 类似,[Symbol.matchAll]() 首先使用 [Symbol.species] 构建一个新的正则表达式,从而避免以任何方式修改原始正则表达式。lastIndex 的初始值是原始正则表达式的值。

js
const regexp = /[a-c]/g;
regexp.lastIndex = 1;
const str = "abc";
Array.from(str.matchAll(regexp), (m) => `${regexp.lastIndex} ${m[0]}`);
// [ "1 b", "1 c" ]

输入是否为全局正则表达式的验证发生在 String.prototype.matchAll() 中。[Symbol.matchAll]() 不会验证输入。如果正则表达式不是全局的,则返回的迭代器会生成一次 exec() 的结果,然后返回 undefined。如果正则表达式是全局的,则每次调用返回的迭代器的 next() 方法时,都会调用正则表达式的 exec() 并生成结果。

当正则表达式是粘性的(sticky)且全局时,它仍然会执行粘性匹配——也就是说,它不会匹配 lastIndex 之外的任何出现。

js
console.log(Array.from("ab-c".matchAll(/[abc]/gy)));
// [ [ "a" ], [ "b" ] ]

如果当前匹配项是空字符串,则 lastIndex 仍会推进。如果正则表达式具有 u 标志,则它会推进一个 Unicode 代码点;否则,它会推进一个 UTF-16 代码点。

js
console.log(Array.from("😄".matchAll(/(?:)/g)));
// [ [ "" ], [ "" ], [ "" ] ]

console.log(Array.from("😄".matchAll(/(?:)/gu)));
// [ [ "" ], [ "" ] ]

此方法用于自定义 RegExp 子类中 matchAll() 的行为。

示例

直接调用

此方法的使用方式与 String.prototype.matchAll() 几乎相同,只是 this 的值和参数的顺序不同。

js
const re = /\d+/g;
const str = "2016-01-02";
const result = re[Symbol.matchAll](str);

console.log(Array.from(result, (x) => x[0]));
// [ "2016", "01", "02" ]

在子类中使用 [Symbol.matchAll]()

RegExp 的子类可以重写 [Symbol.matchAll]() 方法来修改默认行为。

例如,要返回一个 Array 而不是一个 迭代器

js
class MyRegExp extends RegExp {
  [Symbol.matchAll](str) {
    const result = RegExp.prototype[Symbol.matchAll].call(this, str);
    return result ? Array.from(result) : null;
  }
}

const re = new MyRegExp("(\\d+)-(\\d+)-(\\d+)", "g");
const str = "2016-01-02|2019-03-07";
const result = str.matchAll(re);

console.log(result[0]);
// [ "2016-01-02", "2016", "01", "02" ]

console.log(result[1]);
// [ "2019-03-07", "2019", "03", "07" ]

规范

规范
ECMAScript® 2026 语言规范
# sec-regexp-prototype-%symbol.matchall%

浏览器兼容性

另见