You gave your regexp the g flag, which means it will globally match the results. Thus, you explicitly requested your regular expression to keep information about your previous matches.
var regex1 = /a|b/g; > regex1.lastIndex 0 > regex1.test('a'); true > regex1.lastIndex 1 > regex1.test('a'); false
If you remove g , you will get the expected results.
You can check the properties of the .lastIndex expression if it is executed.
source share