When a global search needs both every match and its captures, use RegExp.exec repeatedly. Each call returns the next match and its captures, advancing from the previous call. section 10.4 section 10.4.2
Listing 10.6. Using the exec method to do both capturing and a global search
const html = "<div class='test'><b>Hello</b> <i>world!</i></div>";
const tag = /<(\/?)(\w+)([^>]*?)>/g;
let match, num = 0;
while ((match = tag.exec(html)) !== null) { #A
assert(match.length === 4,
"Every match finds each tag and 3 captures.");
num++;
}
assert(num === 6, "3 opening and 3 closing tags found.");
The HTML tag example uses a global pattern and repeated exec calls to find six tags, with three captures in each result. section 10.4.2
