title: JavaScript: "Dynamic" back reference RegExp replacement
body: <p>I want to make replacements like this:</p>
  
  <pre><code>var txt = "Some text containing $_variable1 and with $_variable2 inside of it as well.";
  var rx = /(\$_[a-z]+)/g
  
  var $_variable1 = "A CAT";
  var $_variable2 = "A HOTDOG";
  
  var replaced_txt = txt.replace(rx, $1);
  </code></pre>
  
  <p>I want <code>replaced_txt</code> to equal "...containing A CAT and with A HOTDOG ins...", but the only way to achieve this that I've found so far is this:</p>
  
  <pre><code>var replaced_txt = txt.replace(rx, function($1){return eval($1)});
  </code></pre>
  
  <p>And I have a feeling this is not the most elegant solution, no?</p>
  
  <p>Preferably I'd like to avoid <code>eval()</code></p>
  
  <p>I'm grateful for any ideas on this!</p>
  
  <p>/C</p>