Skip to content

Instantly share code, notes, and snippets.

@xarg
Created September 15, 2010 09:45
Show Gist options
  • Save xarg/580485 to your computer and use it in GitHub Desktop.
Save xarg/580485 to your computer and use it in GitHub Desktop.
function Linkify(inputText) {
//URLs starting with http://, https://, or ftp://
var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
var replacedText = inputText.replace(replacePattern1, '<a href="$1">$1</a>');
//URLs starting with www. (without // before it, or it'd re-link the ones done above)
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2">$2</a>');
//Change email addresses to mailto:: links
var replacePattern3 = /(^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-\.]+?\.[a-zA-Z]{2,6}$)/gim;
var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText
}
$(document).ready(function(){
$('.stringWidget span').each(function(){
$(this).html(Linkify($(this).html()));
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment