This seems to still be an issue in August of 2021... I just want to share some things I have learned before stumbling upon this question and answer. I was baffled by this problem and had no meaningful way forward - until now.
It doesn't matter whether you use exec() or test() or match(). The regex still doesn't work properly on every other occurrence.
It doesn't matter if you set the regex with
let reg = new RegExp(/<table(\s*[^>]*)>/g);
or with const. Doesn't matter if you set it globally or locally either...
What does work to bypass this problem is wrapping your regex statement in parenthesis in your loop like so:
Object.keys(table).forEach(key => {
if((new RegExp(/<table(\s*[^>]*)>/g)).test(___your test string___)){
//Do what you need to do
}
});
Remove the parenthesis around the Regex, and watch every other one fail....
Thank you so much for the answer @Nick Craver and the comment @prototype!
This exact Regex was what was giving me trouble. It would work for one object, and fail for the subsequent object, and it made no sense. I am only here to say that this is still very relevant in 2021.