What is the meaning of the g flag in regular expressions?
What is is the difference between /.+/g and /.+/?
g -> returns all matcheswithout g -> returns first matchexample:
'1 2 1 5 6 7'.match(/\d+/) returns ["1", index: 0, input: "1 2 1 5 6 7", groups: undefined]. As you see we can only take first match "1". '1 2 1 5 6 7'.match(/\d+/g) returns an array of all matches ["1", "2", "1", "5", "6", "7"]. G in regular expressions is a defines a global search, meaning that it would search for all the instances on all the lines.
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.