Symbol.matchAll

Symbol.matchAll 静态数据属性代表 众所周知的符号 Symbol.matchAllString.prototype.matchAll() 方法在返回迭代器的方法的第一个参数上查找此符号,该迭代器生成当前对象与字符串的匹配项。

¥The Symbol.matchAll static data property represents the well-known symbol Symbol.matchAll. The String.prototype.matchAll() method looks up this symbol on its first argument for the method that returns an iterator, that yields matches of the current object against a string.

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

¥For more information, see RegExp.prototype[Symbol.matchAll]() and String.prototype.matchAll().

Try it

¥Value

众所周知的符号 Symbol.matchAll

¥The well-known symbol Symbol.matchAll.

Property attributes of Symbol.matchAll
Writable no
Enumerable no
Configurable no

示例

¥Examples

使用 Symbol.matchAll

¥Using 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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看