JS: RegExp. prototype [Symbol.matchAll]
(new in ECMAScript 2020)
What is RegExp.prototype symbol.matchAll
RegExp.prototype[Symbol.matchAll] is the method that defines how String.prototype.matchAll works.
This means, you can customize String.prototype.matchAll by adding a symbol property Symbol.matchAll function to the regex object.
// these are equivalent console.log(Array.from( /a/g[Symbol.matchAll]("cat"), )); // [ [ "a", index: 1, input: "cat", groups: undefined ] ] console.log(Array.from( "cat".matchAll(/a/g), )); // [ [ "a", index: 1, input: "cat", groups: undefined ] ]
Example. customized symbol.matchAll
const xRegex = /a/g; // customized matchAll function. always return false xRegex[Symbol.matchAll] = () => { console.log("am called."); return false; }; console.log("a".matchAll(xRegex)); // am called. // false