regex - Advanced Regular Expressions in Javascript -
i'm developing multiple advanced expressions detects 4 things: urls, twitter usernames, hashtags , hex-colors. works if put same thing, example, 4 different hashtags, urls or colors. when put, example hashtag , different thing url hashtag disappears. here can see regularexp:
var regularexp = { twitter: /(^|[,\s])@(\w{1,15})/g, color: /(^|[,\s])#((?:[a-fa-f0-9]){3}|(?:[a-fa-f0-9]){6})/g, hashtag: /(^|[,\s])#(\w{1,15})/g, url: /(^|\s)(((http(s)?:\/\/|ftp(s)?:\/\/)?([a-za-z0-9_-]+\.)?(?:[a-za-z0-9]+)(?:\.[a-z]{2,4}){1,2})(\/.*)*)/g }, replacer = { twitter: "$1<a rel='nofollow' target='_blank' href='http://twitter.com/$2'>@$2</a>", color: "$1<span class='hex-color' style='background-color:#$2 !important'>#$2</span>", hashtag: "$1<a rel='nofollow' target='_blank' href='http://twitter.com/search?q=%23$2&src=hash'>#$2</a>", url: "$1<a rel='nofollow' target='_blank' href='$2'>$2</a>" };
you can try out here: http://jsfiddle.net/esa_u7/krtmw/ write in first textarea: "#fff, #000, #00f" without slashes (as can see worked) , add twitter username @example. why hashtag disappears.
hash tags can used regex delimiters , need escaped \#
Comments
Post a Comment