What is the meaning of the g
flag in regular expressions?
What is is the difference between /.+/g
and /.+/
?
g
is the global search flag.
The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.
So the difference between /.+/g
and /.+/
is that the g
version will find every occurrence instead of just the first.