If I use RegExp test(), it gives the correct answer the first time, but thereafter it returns the inverse result (if the match is found) or just false if it's not:
let regex = new RegExp('bird|dog', 'g')
console.log(regex.test('Imma bird')) // output: true
console.log(regex.test('Imma dog')) // output false !
console.log(regex.test('Imma dog')) // output true
console.log(regex.test('Imma bird')) // output false !?
console.log(regex.test('Imma bird')) // output true
console.log(regex.test('Imma bird')) // output false ??
console.log(regex.test('Imma believer')) // output false
console.log(regex.test('Imma believer')) // output false
console.log(regex.test('Imma believer')) // output false
If I don't use the 'g' qualifier, all works as expected:
let regex = new RegExp('bird|dog')
console.log(regex.test('Imma bird')) // output true
console.log(regex.test('Imma bird')) // output true
console.log(regex.test('Imma believer')) // output false
console.log(regex.test('Imma believer')) // output false
Clearly there is something I don't understand at play here. But what?