Created
October 7, 2012 09:43
-
-
Save dvcrn/3847689 to your computer and use it in GitHub Desktop.
Parse twitter urls, hashtags and usernames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parse_tweet = function (str) { | |
var create_link = function (url, text) { | |
var link = $("<a>", { | |
text: text, | |
href: url, | |
target: "_blank" | |
}); | |
return link.prop('outerHTML'); | |
}; | |
// parse URLs | |
str = str.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function (s) { | |
return create_link(s, s); | |
}); | |
// parse username | |
str = str.replace(/[@]+[A-Za-z0-9_]+/g, function (s) { | |
return create_link("http://twitter.com/" + s.replace('@', ''), s); | |
}); | |
// parse hashtags | |
str = str.replace(/[#]+[A-Za-z0-9_]+/g, function (s) { | |
return create_link("http://search.twitter.com/search?q=" + s.replace('#', ''), s); | |
}); | |
return str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment