Skip to content

Instantly share code, notes, and snippets.

@funkatron
Created April 1, 2010 19:00
Show Gist options
  • Save funkatron/352228 to your computer and use it in GitHub Desktop.
Save funkatron/352228 to your computer and use it in GitHub Desktop.
var msg = "(I did one for my site; see the logo:https://www.deadspin.com/5227676/stafford-welcomed-to-detroit-with-warm-prickly-arms)";
function linkURLs(str) {
var regexp_url = /([^\w])((http(s?):\/\/)|(www\.))(\w+[^\s\)<]+)/gi;
while ((ms = regexp_url.exec(str))) {
if ( /\.$/.test(ms[6]) ) {
period = '.';
ms[6] = ms[6].slice(0, -1);
}
/*
sometimes we can end up with a null instead of a blank string,
so we need to force the issue in javascript.
*/
for (x=0; x<ms.length; x++) {
if (!ms[x]) {
ms[x] = '';
}
}
desc = ms[5]+ms[6];
newstr = ms[1]+'<a href="http'+ms[4]+'://'+ms[5]+ms[6]+'">'+desc+'</a>';
str = str.replace(ms[0], newstr);
}
return str;
}
alert(linkURLs(msg));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment