title: Multiple nested matches in JavaScript Regular Expression
body: <p>Trying to write a regular expression to match GS1 barcode patterns ( <a href="https://en.wikipedia.org/wiki/GS1-128" rel="noreferrer">https://en.wikipedia.org/wiki/GS1-128</a> ), that contain 2 or more of these patterns that have an identifier followed by a certain number of characters of data. </p>
  
  <p>I need something that <strong>matches</strong> this barcode because it contains 2 of the identifier and data patterns:</p>
  
  <p>human readable with the identifiers in parens: (01)12345678901234(17)501200</p>
  
  <p>actual data: <code>011234567890123417501200</code></p>
  
  <p>but <strong>should match not</strong> this barcode when there is only one pattern in:</p>
  
  <p>human readable: (01)12345678901234</p>
  
  <p>actual data: <code>0112345678901234</code></p>
  
  <p>It seems like the following should work:</p>
  
  <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 regex = /(?:01(\d{14})|10([^\x1D]{6,20})|11(\d{6})|17(\d{6})){2,}/g;
  var str = "011234567890123417501200";
  
  console.log(str.replace(regex, "$4"));
  // matches 501200
  console.log(str.replace(regex, "$1"));
  // no match? why?</code></pre>
  </div>
  </div>
  </p>
  
  <p>For some strange reason as soon as I remove the <code>{2,}</code> it works, but I need the <code>{2,}</code> so that it only returns matches if there is more than one match. </p>
  
  <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>// Remove {2,} and it will return the first match
  var regex = /(?:01(\d{14})|10([^\x1D]{6,20})|11(\d{6})|17(\d{6}))/g;
  var str = "011234567890123417501200";
  
  console.log(str.replace(regex, "$4"));
  // matches 501200
  console.log(str.replace(regex, "$1"));
  // matches 12345678901234
  // but then the problem is it would also match single identifiers such as
  var str2 = "0112345678901234";
  console.log(str2.replace(regex, "$1"));
   </code></pre>
  </div>
  </div>
  </p>
  
  <p>How do I make this work so it will only match and pull the data if there is more than 1 set of match groups?</p>
  
  <p>Thanks!</p>