Skip to content

Instantly share code, notes, and snippets.

@mombrea
Created September 17, 2013 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mombrea/6598272 to your computer and use it in GitHub Desktop.
Save mombrea/6598272 to your computer and use it in GitHub Desktop.
JavaScript prototype functions to parse out twitter action items into HTML
//Twitter Parsers
String.prototype.parseURL = function() {
return this.replace(/[A-Za-z]+://[A-Za-z0-9-_]+.[A-Za-z0-9-_:%&~?/.=]+/g, function(url) {
return url.link(url);
});
};
String.prototype.parseUsername = function() {
return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
var username = u.replace("@","")
return u.link("https://twitter.com/"+username);
});
};
String.prototype.parseHashtag = function() {
return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
var tag = t.replace("#","%23")
return t.link("https://twitter.com/search?q="+tag+”&src=hash”);
});
};
@markni
Copy link

markni commented Dec 16, 2014

Thanks for the sharing, the last function should be updated to new address (2014):

String.prototype.parseHashtag = function() {
    return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
        var tag = t.replace("#","")
        return t.link("https://twitter.com/hashtag/"+tag);
    });
};

@kingdido999
Copy link

I think you need to escape forward slashes in parseURL function:

// replace
/[A-Za-z]+://[A-Za-z0-9-_]+.[A-Za-z0-9-_:%&~?/.=]+/g
// to
/[A-Za-z]+:\/\/[A-Za-z0-9-_]+.[A-Za-z0-9-_:%&~?/.=]+/g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment