Symbol.matchAll well-known symbol returns an iterator, that yields matches of the regular expression against a string. This function is called by the String.prototype.matchAll() 方法。

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

描述

This Symbol is used for String.prototype.matchAll() and specifically in RegExp.prototype[@@matchAll]() . The following two examples return same result:

'abc'.matchAll(/a/);
/a/[Symbol.matchAll]('abc');
					

This method exists for customizing match behavior within RegExp subclasses.

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

范例

Using Symbol.matchAll

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"]
					

String.prototype.matchAll() and RegExp.prototype[@@matchAll]() 了解更多范例。

规范

规范
ECMAScript (ECMA-262)
The definition of 'Symbol.matchAll' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
matchAll Chrome 73 Edge 79 Firefox 67 IE No Opera 60 Safari No WebView Android 73 Chrome Android 73 Firefox Android 67 Opera Android 52 Safari iOS No Samsung Internet Android No nodejs 12.0.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: