Javascript regex match Twitter username, but exclude colon -


using twitter api trying link @ symbols persons account eg. @stackoverflow goes https://twitter.com/stackoverflow.

this working below except when looking @ retweets have syntax

rt @stackoverflow: 

the first regex works keeps : remove last character if :. how can match @stackoverflow pattern remove : it. (i don't want remove : whole string may have links in it)

var str = 'rt @stackoverflow: great ...'  	str = str.replace(/@(\s*)/g, '<a target="_blank" href="https://twitter.com/$1">@$1</a>')  	str = str.replace(/:$/,"") //doesn't  	console.log(str) // returns rt <a target="_blank" href="https://twitter.com/stackoverflow:">@stackoverflow:</a> great ...

instead of /@(\s*)/g use /@(\w+)/gi, because twitter usernames confined \w represents: a-z, , 0-9 , _. stated in twitter support article:

a username can contain alphanumeric characters (letters a-z, numbers 0-9) exception of underscores, noted above. check make sure desired username doesn't contain symbols, dashes, or spaces.

var str = 'rt @stackoverflow: great ...'    str = str.replace(/@(\w+)/gi, '<a target="_blank" href="https://twitter.com/$1">@$1</a>')  	  console.log(str) 


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -