The matchAll
method returns all
matches as an iterable object, each
element of which contains an array
of the match and its capturing
groups. The method can only be
called with the g
modifier.
Let's look at an example. Let's say we have the following string:
let str = '12 34 56';
We find all pairs of numbers and split their numbers into capturing groups:
let matches = str.matchAll(/(\d)(\d)/g);
Let's go through the resulting iterable in a loop and display the found matches:
for (let match of matches) {
console.log(match); // [12, 1, 2], [34, 3, 4], [56, 5, 6]
}
Given the following string:
let str = '12:37 15:48 17:59';
Find in it all substrings with the time and for each found put the hours and minutes into capturing groups.
Given a string:
let str = 'site.ru sss site.com zzz site.net';
Get an array of domain names from this string, put the domain name and its zone in different capturing groups in this array.