javascript highlight a string
<p><code>var str = 'test TEST';</code></p>

<p>How to get <code>str</code> = '<code>&lt;span class="red"&gt;test&lt;/span&gt; &lt;span class="red"&gt;TEST&lt;/span&gt;</code>' in return.</p>

<p>Thanks a lot.</p>

<p>=============The correct answer=================</p>

<p>Idea from <code>Tim Down</code></p>

<pre><code>var highlighted = str.replace(/(test)/gi, '&lt;span class="red"&gt;$1&lt;/span&gt;');
</code></pre>

<p><code>&lt;span class="red"&gt;test&lt;/span&gt; &lt;span class="red"&gt;TEST&lt;/span&gt;</code></p>
