title: jquery: how to use regex to wrap a substring with html some markup and insert variable?
body: <p>I am trying to </p>
  
  <ol>
  <li>find substring(s) that match an expression ((my substring))</li>
  <li>wrap the results with markup and </li>
  <li>inject different vars on the different results.</li>
  </ol>
  
  <p>It works for one substring:</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 link1 = "URL_1",
      myRegex = /(\(\(.*\)\))/g,
      myOldString = "lorem ((ipsum dolor)) sit amet consectur";
  
  var myNewString = myOldString.replace(myRegex, "&lt;a href='"+link1+"' class='red'&gt;$1&lt;/a&gt;");
  $('.mytext').html(myNewString);</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.red {
    color:red;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;h2 class="mytext"&gt;&lt;/h2&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>But I can't get it to work with multiple results:</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 link1 = "URL_1",
      link2 = "URL_2",
      myRegex = /(\(\(.*\)\))/g,
      myOldString = "lorem ((ipsum dolor)) sit ((amet)) consectur";
  
  var myNewString = myOldString.replace(myRegex, "&lt;a href='"+link1+"' class='red'&gt;$1&lt;/a&gt;", "&lt;a href='"+link2+"' class='red'&gt;$2&lt;/a&gt;");
  
  
  $('.mytext').html(myNewString);</code></pre>
  </div>
  </div>
  </p>
  
  <p>Any hint?</p>