Pass regex replace match to a function
<p>I want to add a span tag with a class to style with css around content in a string that matches a regex, and is in a set of accepted values.</p>

<p>For instance with a regex <code>/fo./</code> I'd want to turn <code>barfoobarfoafoc</code> into </p>

<pre><code>bar&lt;span class = "stylename"&gt;foo&lt;/span&gt;bar&lt;span class = "stylename"&gt;foa&lt;/span&gt;foc
</code></pre>

<p>if both foo and foa are in the set of accepted strings, but foc is not.</p>

<p>I've played around with String.replace(), but I can't find the way to get the matched content into a verification function So I tried:</p>

<pre><code>output = output.replace(/fo./g,isAcceptedValue("$&amp;"));
</code></pre>

<p>but the parameter passed to replace is just "$&amp;" rather than the matched string.  Which probably shouldn't have been surprising.  Is there a way to do this using the replace function?  Or am I going to have to manually get each regex return and replace the substring from the start of the match and all of that?  I was hoping there was a way to do this during replace so the code is clear about what its doing and I don't have to deal with calculating the substrings.</p>
