title: Why does a RegExp with global flag give wrong results?
body: <p>What is the problem with this regular expression when I use the global flag and the case insensitive flag? Query is a user generated input. The result should be [true, true].</p>
  
  <pre><code>var query = 'Foo B';
  var re = new RegExp(query, 'gi');
  var result = [];
  result.push(re.test('Foo Bar'));
  result.push(re.test('Foo Bar'));
  // result will be [true, false]
  </code></pre>
  
  <hr>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var reg = /^a$/g;
  for(i = 0; i++ &lt; 10;)
     console.log(reg.test("a"));</code></pre>
  </div>
  </div>
  </p>