 <p>You don't have to capture the <code>Horsie</code> or the <code>Birdie</code>.</p> <pre><code>s/Horsie(Doggie)Birdie/$1/g; </code></pre> <p>A similar thing should work for Javascript as well. This is probably as efficient as it gets, and at least as fast as using look-around assertions; although you should benchmark it if you want to know for sure. (The results, of course, will depend on the horsies, doggies and birdies in question.)</p> <p>Mandatory disclaimer: you should know <a href="http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">what happens</a> when you use regular expressions with HTML...</p>