I am looking to replace any @mention in a string with <a href="http://twitter.com/mention">@mention</a> using Javascript or jQuery. I have the function from the second answer of How to replace plain URLs with links? and am simply looking to add a further replacePattern
            Asked
            
        
        
            Active
            
        
            Viewed 2,231 times
        
    2
            
            
        1 Answers
9
            function replaceAtMentionsWithLinks ( text ) {
    return text.replace(/@([a-z\d_]+)/ig, '<a href="http://twitter.com/$1">@$1</a>'); 
}
See it here in action: http://jsfiddle.net/h6HMY/
 
    
    
        Joseph Silber
        
- 214,931
- 59
- 362
- 292
- 
                    Thankyou very much - just what I wanted. – Sam Jun 02 '13 at 03:56
- 
                    Hi; this doesn't work if someone's username has an underscore (only highlights the section before the underscore). Any ideas on how to solve? – Sam Jun 05 '13 at 12:44
- 
                    hey @JosephSilber do you have any idea on how to detect arabic and latin names? – zianwar Sep 30 '15 at 18:54
- 
                    2@zianwar - You have to tweak the regex: `/@([a-z\u0600-\u06ff\u0750-\u077f\ufb50-\ufbc1\ufbd3-\ufd3f\ufd50-\ufd8f]|[\ufd92-\ufdc7\ufe70-\ufefc\uFDF0-\uFDFD\d_]+)/ig` – Joseph Silber Oct 01 '15 at 14:43
 
    