var matches;
while(matches = /./g.exec("abc"))
{
console.log("hey");
}
This never terminates. I expect it to terminate after 3 loops.
Warning: Don't run it in Chrome because the infinite log lines freeze your entire system. It's safe to run it in IE (it still freezes your webpage but you can go to the location bar and press enter to reload).
matches = /./g.exec("abc")will return ['a'] which is a truthy value in JS, hence it runs into an infinite loop. It's likewhile(true){ // dosomething }. As the condition evaluates to true in every iteration, it will never breakgflag, each iteration is supposed to start where the previous one ended."abc"with a fresh regexp each time. For sure this is not what was intended, but you have to observe where the regexp is created and compiled. Even if javascript optimices this to create and compile the regexp only once, the matching status or history must be renewed each time you go through the test.