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 |
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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
Symbol
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()