title: Regular expression only returning first group
body: <p>Probably something simple but i am trying to return the capture groups from this regex...</p>
  
  <pre><code>const expression = /^\/api(?:\/)?([^\/]+)?\/users\/([^\/]+)$/g
  </code></pre>
  
  <p>The code i am using to do this is the following...</p>
  
  <pre><code>const matchExpression = (expression, pattern) =&gt; {
  
      let match;
      let matches = [];
  
      while((match = expression.exec(pattern)) != null) {
           matches.push(match[1]);
      };
  
      return matches;
  };
  </code></pre>
  
  <p>I am expecting the following result when matched against <code>/api/v1/users/1</code>...</p>
  
  <pre><code>['v1', '1']
  </code></pre>
  
  <p>But instead only seem to get one result which is always the first group.</p>
  
  <p>The expression itself is fine and has been tested across multiple services but can't seem to figure out why this is not working as expected.</p>
  
  <p>Any help would be hugely appreciated.</p>