Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace @mention with link

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

like image 811
Sam Avatar asked Jun 03 '26 08:06

Sam


1 Answers

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/

like image 81
Joseph Silber Avatar answered Jun 05 '26 00:06

Joseph Silber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!