title: Interesting test of Javascript RegExp
body: <p>I wrote a Javascript RegExp test to detect date string format, I added an redundant "g" flag by mistake and found something interesting.</p>
  
  <pre class="lang-js prettyprint-override"><code>var s = "2009/03/10";
  var regex=/^\d{4}[/]\d{2}[/]\d{2}$/g;
  alert(regex.test(s));
  alert(regex.test(s));
  alert(regex.test(s));
  alert(regex.test(s));
  </code></pre>
  
  <p>I got a 'true' followed by a 'false', then another 'true', then another 'false'.</p>
  
  <p>If I use a loop to execute it, I found something more interesting, I got four "true" in IE and Safari, and true,false,true,false in FF, Chrome.</p>
  
  <pre class="lang-js prettyprint-override"><code>for (var i=0; i&lt;4; i++)
  {
    var s = "2009/03/10";
    var regex=/^\d{4}[/]\d{2}[/]\d{2}$/g;
    alert(regex.test(s));
  }
  </code></pre>
  
  <p>Does anybody has idea to explain why the Javascript regex behaves like that and what cause browsers return different results? ( related to variable declaration and life scope? )</p>