Skip to content

Instantly share code, notes, and snippets.

@jcutrell
Created March 14, 2012 20:27
Show Gist options
  • Save jcutrell/2039271 to your computer and use it in GitHub Desktop.
Save jcutrell/2039271 to your computer and use it in GitHub Desktop.
Replace Links
//utility for detecting links in text string
detectLinks(string, newtab){
newtab = (newtab == undefined) ? true : newtab;
var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
return newtab ? string.replace(regexp, "<a href='$1' target='_blank'>$1</a>") : string.replace(regexp, "<a href='$1'>$1</a>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment