It seems like when using match
without the g
lobal flag, it returns an object:
// Chrome dev tools
console.log("Hello Hello".match(/el*/));
VM499:1 ['ell', index: 1, input: 'Hello Hello', groups: undefined]
However, if I use the g
flag, regardless of whether there is one or more results, instead of returning an object, it returns an array:
console.log("Hello Hello".match(/ello$/g));
VM512:1 ['ello']
console.log("Hello Hello".match(/el*/g));
VM550:1 (2) ['ell', 'ell']
Why does javascript convert a match that uses g
to an array of strings? Perhaps if you’re looking for multiple matches you should only be using matchAll
?