Remove the 'g' flag. When you use the 'g' flag, it updates the lastIndex property of the regex (preparing for a successive search on the same string) and then starts the next search from that index value (thus giving you a false reading on your next search).
Similar question and answer here: Why is Regex Javascript //g flag affecting state?