Skip to content

Instantly share code, notes, and snippets.

@dvcrn
Created October 7, 2012 09:43
Show Gist options
  • Save dvcrn/3847689 to your computer and use it in GitHub Desktop.
Save dvcrn/3847689 to your computer and use it in GitHub Desktop.
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