Skip to content

Instantly share code, notes, and snippets.

@cuth
Created April 28, 2014 20:15
Show Gist options
  • Save cuth/11382782 to your computer and use it in GitHub Desktop.
Save cuth/11382782 to your computer and use it in GitHub Desktop.
Methods to format tweets with html.
(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