What is the problem with this regular expression when I use the global flag and the case insensitive flag? Query is a user generated input. The result should be [true, true].
var query = 'Foo B';
var re = new RegExp(query, 'gi');
var result = [];
result.push(re.test('Foo Bar'));
result.push(re.test('Foo Bar'));
// result will be [true, false]
var reg = /^a$/g;
for(i = 0; i++ < 10;)
console.log(reg.test("a"));
A
RegExpobject with thegflag keeps track of thelastIndexwhere a match occurred, so on subsequent matches it will start from the last used index, instead of 0. Take a look:If you don’t want to manually reset
lastIndexto 0 after every test, just remove thegflag.Here’s the algorithm that the specs dictate (section 15.10.6.2):