What is the meaning of the ‘g’ flag in regular expressions?

g is for global search. Meaning it’ll match all occurrences. You’ll usually also see i which means ignore case.

Reference: global – JavaScript | MDN

The “g” flag indicates that the regular expression should be tested against all possible matches in a string.

Without the g flag, it’ll only test for the first.

Leave a Comment