2 Answers
2

If your regular expression uses the "g" flag, you can use the exec
method multiple times to find successive matches in the same string.
When you do so, the search starts at the substring of str specified by
the regular expression's lastIndex property

So when your regex has a g flag, and you use the exec method once, the next time you execute it it will search for a match after the first match. In this case, there is none: exec will return null and the lastIndex property will be reset.