2

Hi I noticed a very interesting behavior in JavaScript RegEx.exec function. if I run the same regex multiple times on a string that returns positive, it will return positive only half of the time.. (I don't need to use .test) I do need to capture some values from an input string.

I am using Chrome 45.0.2454.93.

Chrome console output

        var x = new RegExp("12|11","gi");
        console.log(x.exec("11,1,5,0")); // OK returns 11
        console.log(x.exec("11,1,5,0")); // NULL
        console.log(x.exec("11,1,5,0")); // OK returns 11
        console.log(x.exec("11,1,5,0")); // NULL
        console.log(x.exec("11,1,5,0")); // OK returns 11
        console.log(x.exec("11,1,5,0")); // NULL
    

I will up-vote a good explanation of this behavior.

Regards Erik.

Erik Simonic
  • 457
  • 3
  • 13
  • 2
    [just read the doc](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Finding_successive_matches) – Hacketo Sep 17 '15 at 12:48
  • 1
    True, I guess it is something that can bugle your mind if you are coming from a C# programming language.. – Erik Simonic Sep 17 '15 at 12:52

0 Answers0