title: JavaScript regular expressions and sub-matches
body: <p>Why do Javascript sub-matches stop working when the <code>g</code> modifier is set?</p>
  
  <pre><code>var text = 'test test test test';
  
  var result = text.match(/t(e)(s)t/);
  // Result: ["test", "e", "s"]
  </code></pre>
  
  <p>The above works fine, <code>result[1]</code> is <code>"e"</code> and <code>result[2]</code> is <code>"s"</code>.</p>
  
  <pre><code>var result = text.match(/t(e)(s)t/g);
  // Result: ["test", "test", "test", "test"]
  </code></pre>
  
  <p>The above ignores my capturing groups. Is the following the only valid solution?</p>
  
  <pre><code>var result = text.match(/test/g);
  for (var i in result) {
      console.log(result[i].match(/t(e)(s)t/));
  }
  /* Result:
  ["test", "e", "s"]
  ["test", "e", "s"]
  ["test", "e", "s"]
  ["test", "e", "s"]
  */
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I am back again to happily tell you that 10 years later you can now do this (.matchAll has been added to the spec)</p>
  
  <pre><code>let result = [...text.matchAll(/t(e)(s)t/g)];
  </code></pre>