title: Javascript replace with reference to matched group?
body: <p>I have a string, such as <code>hello _there_</code>. I'd like to replace the two underscores with <code>&lt;div&gt;</code> and <code>&lt;/div&gt;</code> respectively, using <strong>JavaScript</strong>. The output would (therefore) look like <code>hello &lt;div&gt;there&lt;/div&gt;</code>. The string might contain multiple pairs of underscores.</p>
  
  <p>What I am looking for is a way to <strong>either</strong> run a function on each match, the way Ruby does it:</p>
  
  <pre><code>"hello _there_".gsub(/_.*?_/) { |m| "&lt;div&gt;" + m[1..-2] + "&lt;/div&gt;" }
  </code></pre>
  
  <p><strong>Or</strong> be able to reference a matched group, again the way it can be done in ruby:</p>
  
  <pre><code>"hello _there_".gsub(/_(.*?)_/, "&lt;div&gt;\\1&lt;/div&gt;")
  </code></pre>
  
  <p>Any ideas or suggestions?</p>