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.

Symbol.matchAll 内置通用(well-known)符号指定方法返回一个迭代器,该迭代器根据字符串生成正则表达式的匹配项。此函数可以被 String.prototype.matchAll() 方法调用。

详细信息请参阅 RegExp.prototype[Symbol.matchAll]()String.prototype.matchAll()

尝试一下

内置通用符号 Symbol.matchAll

Symbol.matchAll 的属性特性
可写
可枚举
可配置

示例

使用 Symbol.matchAll

js
const 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)));
// ["2016", "01", "02", "2019", "03", "07"]

规范

Specification
ECMAScript Language Specification
# sec-symbol.matchall

浏览器兼容性

BCD tables only load in the browser

参见