title: How to replace all occurences of a variable in a string using javascript?
body: <p>I'm trying to replace all the occurrences of a variable in a string using javascript. </p>
  
  <p>This is not working.:</p>
  
  <pre><code>var id = "__1";
  var re = new RegExp('/' + id + '/g');
  var newHtml = oldHtml.replace( re, "__2");
  </code></pre>
  
  <p>This is only replacing the first occurrence of id:</p>
  
  <pre><code>var id = "__1";
  var newHtml = oldHtml.replace( id,"__2");
  </code></pre>
  
  <p>What am I doing wrong here?</p>
  
  <p>Thanks</p>