Skip to content

Instantly share code, notes, and snippets.

@dahabit
Forked from pamelafox/linkify.js
Created March 3, 2013 01:29
Show Gist options
  • Save dahabit/5074068 to your computer and use it in GitHub Desktop.
Save dahabit/5074068 to your computer and use it in GitHub Desktop.
function linkifyText(string){
if (string) {
string = string.replace(
/((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
function(url){
var full_url = url;
if (!full_url.match('^https?:\/\/')) {
full_url = 'http://' + full_url;
}
return '<a target="_blank" href="' + full_url + '">' + url.substring(0, Math.min(full_url.length, 20)) + '...</a>';
});
}
return string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment