Why is the JavaScript RegExp.test() method behaving as a toggle?
Your regex has the global (g
) flag set. Every time it is executed, it'll update an internal index (the lastIndex property) specifying where it left off, and begin searching at that point the next time through.
Of course, you don't really want that - you want it to start at the beginning every time. So ditch the g
flag.