JavaScript String matchAll() method
returns an iterator of results after matching a string against a regular expression

matchAll() method returns an iterator containing the results of matching a string against a regular expression.

syntax

matchAll(regexp)

regexp A regular expression object, or any object that has a Symbol.match method.

If regexp is not a RegExp object and does not have a Symbol.match method, it is implicitly converted to a RegExp.

If the search value is a regex, then it must have the global g flag set, or a TypeError is thrown.

If the search value is a string, it is converted to a regular expression.

matchAll() method returns an iterator ↴

To convert the iterator to an array, use any of the following ↴

Array.from(str.matchAll(regexp)) or

[...str.matchAll(regexp)] or

for (result of (str.matchAll(regExp)))

matchAll() method must have the global g flag set, otherwise TypeError is thrown.

matchAll() method returns an empty array [ ] if no match is found.

matchAll() returns an iterator that has an array for each match ↴

The returned item will have the following additional properties ↴

index The index of search where the result was found.

input A copy of the search string.

groups An object of named capturing groups having keys as the names and values as the captured matches.

const myString = 'Hello World';

Array.from(myString.matchAll(/l/g)); returns ↴

['l', index: 2, input: 'Hello World', groups: undefined]

['l', index: 3, input: 'Hello World', groups: undefined]

['l', index: 9, input: 'Hello World', groups: undefined]

syntax description
match myString against a regular expression or string
myString.matchAll(regexp)