I had another question and although it was answered i dont understand WHY the regex is affected the way it is
From w3schools it says
g: Perform a global match (find all matches rather than stopping after the first match)
Ok sure. I understand. Thats why i get an array in this code
var str="The rain in SPAIN stays mainly in the plain";
var patt1=/ain/gi;
document.write(str.match(patt1));
Output:
ain,AIN,ain,ain
Regex is similar, with /g
it will replace more than one instance.
However in match
var re=/hi/gi;
alert(re.test("hi") + " " + re.test("hi"));
The result is "true false".
Now why the $%^& does it do that? The string in both test are exactly the same! In the past i thought global meant it will search across newlines (which is what i wanted to do in this test). The very first thing i quoted was about g being a global match.
Nothing makes any reference about it affecting the NEXT CALL! without /g the code will work correctly (also i dont need to go across newlines). WHY is it affecting the next test? gumbo answer makes mention it affects the lastIndex across calls and what the %^&* i had no idea there is shared state as the other two functions made no use of it while i used the g flag. I only wanted a true and false but if anything shouldnt match return an int containing the amount of matches it found globally? (ie 1 in "hi" but 2 in the string "hihi").
Why the heck is g affecting my next call when doing regex.test?! Also if you can, provide when i'd actually want that 'feature'