title: Regex to capture Ids from text
body: <p>I have the following regex where I am trying to capture the Ids of each start comment. But for some reason I am only able to capture the first one. It won't grab the Id of the nested comment. It only prints 1000 to the console. I am trying to get it to capture both 1000 and 2000. Can anyone spot the error in my regex?</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      function ExtractText() {
          var regex = /\&lt;!--Start([0-9]{4})--&gt;([\s\S]*?)&lt;!--End[0-9]{4}--&gt;/gm;
         var match;
          while (match = regex.exec($("#myHtml").html())) {
              console.log(match[1]);
          }
      }
  
  &lt;/script&gt;
  
  &lt;div id="myHtml"&gt;
     &lt;!--Start1000--&gt;Text on&lt;!--Start2000--&gt;the left&lt;!--End1000--&gt;Text on the right&lt;!--End2000--&gt;
  &lt;/div&gt; 
  </code></pre>
  
  <p>Based on Mike Samuel's answer I updated my JS to the following:</p>
  
  <pre><code>function GetAllIds() {
  
          var regex = /&lt;!--Start([0-9]{4})--&gt;([\s\S]*?)&lt;!--End\1--&gt;/g;
          var text = $("#myHtml").html();
          var match;
          while (regex.test(text)) {
              text = text.replace(
                 regex,
                 function (_, id, content) {
                     console.log(id);
                     return content;
                 });
          }
      }
  </code></pre>