Skip to content

Instantly share code, notes, and snippets.

@dvcrn

dvcrn/gist:3847689

Created Oct 7, 2012
Embed
What would you like to do?
Parse twitter urls, hashtags and usernames
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