The exec method in JavaScript regexes

The exec method performs a string search. The found substring and its capturing groups are returned as a result. In this case, each subsequent call to this method will start the search from the position where the previous found substring ended.

Let's look at an example. Let's say we have the following string:

javascriptlet str = '12 34 56';

And we have the following regular expression:

javascriptlet reg = /\d+/g;

Let's sequentially call our method for our string:

javascriptlet res1 = reg.exec(str);
console.log(res1[0]); // 12

let res2 = reg.exec(str);
console.log(res2[0]); // 34

let res3 = reg.exec(str);
console.log(res3[0]); // 56

After three calls, since there are no more matches with regex in our string, the next method call will return null:

javascriptlet res4 = reg.exec(str);
console.log(res4); // null

It is convenient to use this feature of the method in a loop:

javascriptlet str = '12 34 56';
let reg = /\d+/g;

let res;
while (res = reg.exec(str)) {
  console.log(res); // [12], [34], [56] 
}

You can find not only a match, but also put it in your capturing groups:

javascriptlet str = '12 34 56';
let reg = /(\d)(\d)/g;

let res;
while (res = reg.exec(str)) {
  console.log(res); // [12, 1, 2], [34, 3, 4], [56, 5, 6] 
}
Задание 1

Given the following string:

javascriptlet str = '12:37:57 15:48:58 17:59:59';

Find in it all substrings with the time and for each found put the hours, minutes and seconds into capturing groups.