What is the meaning of the 'g' flag in regular expressions?
JavascriptRegexJavascript Problem Overview
What is the meaning of the g
flag in regular expressions?
What is is the difference between /.+/g
and /.+/
?
Javascript Solutions
Solution 1 - Javascript
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.
Solution 2 - Javascript
Example in Javascript to explain:
> 'aaa'.match(/a/g)
[ 'a', 'a', 'a' ]
> 'aaa'.match(/a/)
[ 'a', index: 0, input: 'aaa' ]
Solution 3 - Javascript
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.
Solution 4 - Javascript
As @matiska pointed out, the g
flag sets the lastIndex
property as well.
A very important side effect of this is if you are reusing the same regex instance against a matching string, it will eventually fail because it only starts searching at the lastIndex
.
// regular regex
const regex = /foo/;
// same regex with global flag
const regexG = /foo/g;
const str = " foo foo foo ";
const test = (r) => console.log(
r,
r.lastIndex,
r.test(str),
r.lastIndex
);
// Test the normal one 4 times (success)
test(regex);
test(regex);
test(regex);
test(regex);
// Test the global one 4 times
// (3 passes and a fail)
test(regexG);
test(regexG);
test(regexG);
test(regexG);
Solution 5 - Javascript
There is no difference between /.+/g
and /.+/
because they will both only ever match the whole string once. The g
makes a difference if the regular expression could match more than once or contains groups, in which case .match()
will return an array of the matches instead of an array of the groups.
Solution 6 - Javascript
Beside already mentioned meaning of g
flag, it influences regexp.lastIndex
property:
> The lastIndex is a read/write integer property of regular expression > instances that specifies the index at which to start the next match. > (...) This property is set only if the regular expression instance > used the "g" flag to indicate a global search.
Reference: Mozilla Developer Network
Solution 7 - Javascript
g
->
returns all matcheswithout g
->
returns first match
example:
'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"]
.
Solution 8 - Javascript
G in regular expressions is a defines a global search, meaning that it would search for all the instances on all the lines.
Solution 9 - Javascript
Will give example based on string. If we want to remove all occurences from a string. Lets say if we want to remove all occurences of "o" with "" from "hello world"
"hello world".replace(/o/g,'');