JavaScript has a regular expression class RegExp
. You can create them directly re = new RegExp(...)
or indirectly re = /.../
Most of the traditional methods are what I'm used to from many years of programming
const match = re.match(str);
const isMatch = re.text(str);
But today I looked for a matchAll function and to use it the syntax is this
const matches = re[Symbol.matchAll](str)
What's up with this style of finding a function? Why is it not just
const matches = re.matchAll(str);
I'm guessing there is some reason there are a several functions using this special format. What is the reasoning behind it?
const re = /a(.)b(.)c/g;
const matches = re[Symbol.matchAll]('a1b2c a3b4c a5b6c');
console.log([...matches]);