Here is a codesample for the flag ignore case. I was expected to receive only one match.
var str = "Sample text";
var a = new Array();
a = str.match(/S(am)(p)/i);
result
a = [Samp] [am] [p]
I was expected to have a = [Samp]
if you change i flag with g
var str = "Sample text";
var a = new Array();
a = str.match(/S(am)(p)/g);
surprise (at least for me) the result has only one element
a = [Samp]
The javascript regex API is extremely unintuitive as it does all sorts of magic depending on the
g-flag.I am just gonna cover how
.matchbehaves:Without
g-flag.matchreturns an array of full match plus all the capture groups ornullin case of no match.With
g-flag.matchreturns an array of all the full matches and capture groups don’t make a difference.nullif there are no matches.