Created
April 28, 2014 20:15
-
-
Save cuth/11382782 to your computer and use it in GitHub Desktop.
Methods to format tweets with html.
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
(function () { | |
'use strict'; | |
var userRegex = /(^|\s)(@(\w){1,15})/gi; | |
var urlRegex = /(^|\s)(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))/gi; | |
var linkUsers = function (tweet) { | |
return tweet.replace(userRegex, function ($m, $1, $2) { | |
return $1 + '<a href="https://twitter.com/' + $2 + '" target="_blank">' + $2 + '</a>'; | |
}); | |
}; | |
var linkUrl = function (tweet) { | |
return tweet.replace(urlRegex, function ($m, $1, $2) { | |
return $1 + '<a href="' + $2 + '" target="_blank">' + $2 + '</a>'; | |
}); | |
}; | |
var formatTweet = function (tweet) { | |
tweet = linkUsers(tweet); | |
tweet = linkUrl(tweet); | |
return tweet; | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment