Symbol.matchAll
Symbol.matchAll
は、文字列に対する正規表現の一致を生成するイテレーターを返します。この関数は 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.
説明
このシンボルは String.prototype.matchAll()
、特に RegExp.prototype[@@matchAll]()
で使用されます。以下の 2 つの例は同じ結果を返します。
'abc'.matchAll(/a/);
/a/[Symbol.matchAll]('abc');
このメソッドは、RegExp
サブクラス内の一致動作をカスタマイズするために存在します。
Symbol.matchAll のプロパティ属性 |
|
---|---|
書込可能 | 不可 |
列挙可能 | 不可 |
設定可能 | 不可 |
例
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()
と RegExp.prototype[@@matchAll]()
を参照してください。
仕様
ブラウザー実装状況
BCD tables only load in the browser