Create anchor links from tweet text
<script type="text/javascript">
// Convert URLs (w/ or w/o protocol), @mentions, and #hashtags into anchor links
function twitterLinks(text)
{
var base_url = 'http://twitter.com/'; // identica: 'http://identi.ca/'
var hashtag_part = 'search?q=#'; // identica: 'tag/'
// convert URLs into links
text = text.replace(
/(>|<a[^<>]+href=['"])?(https?:\/\/([-a-z0-9]+\.)+[a-z]{2,5}(\/[-a-z0-9!#()\/?&.,]*[^ !#?().,])?)/gi,
function($0, $1, $2) {
return ($1 ? $0 : '<a href="' + $2 + '" target="_blank">' + $2 + '</a>');
});
// convert protocol-less URLs into links
text = text.replace(
/(:\/\/|>)?\b(([-a-z0-9]+\.)+[a-z]{2,5}(\/[-a-z0-9!#()\/?&.]*[^ !#?().,])?)/gi,
function($0, $1, $2) {
return ($1 ? $0 : '<a href="http://' + $2 + '">' + $2 + '</a>');
});
// convert @mentions into follow links
text = text.replace(
/(:\/\/|>)?(@([_a-z0-9\-]+))/gi,
function($0, $1, $2, $3) {
return ($1 ? $0 : '<a href="' + base_url + $3
+ '" title="Follow ' + $3 + '" target="_blank">@' + $3
+ '</a>');
});
// convert #hashtags into tag search links
text = text.replace(
/(:\/\/[^ <]*|>)?(\#([_a-z0-9\-]+))/gi,
function($0, $1, $2, $3) {
return ($1 ? $0 : '<a href="' + base_url + hashtag_part + $3
+ '" title="Search tag: ' + $3 + '" target="_blank">#' + $3
+ '</a>');
});
return text;
}
</script>Written by Todd
Related protips
2 Responses
Thank You Sir For your !!! EXCELLENT !!! Post.
!!! Thanks A Lot !!!
over 1 year ago
·
@devpuja1988 You're welcome; glad I could help!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Twitter
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#