[@@matchAll]
方法返回对字符串使用正则表达式的所有匹配项。
{{EmbedInteractiveExample("pages/js/regexp-prototype-@@matchall.html")}}
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.
语法
regexp[Symbol.matchAll](str)
参数
-
str
-
一个
String
的匹配对象。
返回值
一个迭代器。
描述
本方法在String.prototype.matchAll()
中被内部调用。例如,以下两个示例返回相同的结果。
'abc'.matchAll(/a/); /a/[Symbol.matchAll]('abc');
本方法用于自定义RegExp
子类中的匹配行为。
示例
直接调用
本方法的使用方法几乎与String.prototype.matchAll()
相同,除了this
的不同以及参数顺序的的差异。
var re = /[0-9]+/g;
var str = '2016-01-02';
var result = re[Symbol.matchAll](str);
console.log(
);
// ["2016", "01", "02"]
在子类中使用@@matchAll
class MyRegExp extends RegExp { [Symbol.matchAll](str) { var result = RegExp.prototype[Symbol.matchAll].call(this, str); if (!result) { return null; } else { return Array.from(result); } } } var re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)', 'g'); var str = '2016-01-02|2019-03-07'; var result = str.matchAll(re); console.log(result[0]); // [ "2016-01-02", "2016", "01", "02" ] console.log(result[1]); // [ "2019-03-07", "2019", "03", "07" ]
浏览器兼容性
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.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@matchAll |
Chrome Full support 73 | Edge No support No | Firefox Full support 67 | IE No support No | Opera Full support 60 | Safari No support No | WebView Android Full support 73 | Chrome Android Full support 73 | Firefox Android Full support 67 | Opera Android Full support 52 | Safari iOS No support No | Samsung Internet Android Full support 5.0 | nodejs Full support 12.0.0 |
Legend
- Full support
- Full support
- No support
- No support