title: RegEx: Link Twitter-Name Mentions to Twitter in HTML
body: <p>I want to do <a href="https://stackoverflow.com/questions/740590/regexp-how-to-extract-usernames-out-of-tweets-twitter-com">THIS</a>, just a little bit more complicated:</p>
  
  <p>Lets say, I have an HTML input:</p>
  
  <pre><code>&lt;a href="http://www.example.com" title="Bla @test blubb"&gt;Don't break!&lt;/a&gt;
  Some Twitter Users: @codinghorror, @spolsky, @jarrod_dixon and @blam4c.
  You can't reach me at blam4c@example.com.
  </code></pre>
  
  <p>Is there a good RegEx to replace the twitter username mentions by links to twitter, but leave @example (eMail-Adress at the bottom) AND @test (in the link title, i.e. in HTML tags)?</p>
  
  <p>It probably should also try to not add links inside existing links, i.e. not break this:</p>
  
  <pre><code>&lt;a href="http://www.example.com"&gt;Hello @someone there!&lt;/a&gt;
  </code></pre>
  
  <p>My current attempt is to add ">" at the beginning of the string, then use this RegEx:</p>
  
  <pre><code>Search:  '/&gt;([^&lt;]*\s)\@([a-z0-9_]+)([\s,.!?])/i'
  Replace: '&gt;\1&lt;a href="http://twitter.com/\2"&gt;@\2&lt;/a&gt;\3'
  </code></pre>
  
  <p>Then remove the ">" I added in step 1.</p>
  
  <p>But that won't match anything but the "@blam4c". I know WHY it does so, that's not the problem.</p>
  
  <p>I would like to find a solution that finds and replaces all twitter user name mentions without destroying the HTML. Maybe it might even be better to code this without RegEx?</p>