Skip to content

Instantly share code, notes, and snippets.

@mathdroid
Created July 2, 2015 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathdroid/c5e51a3784dcafd4d16c to your computer and use it in GitHub Desktop.
Save mathdroid/c5e51a3784dcafd4d16c to your computer and use it in GitHub Desktop.
function urlify(text) {
var urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, function(url) {
return '<a href="' + url + '">' + url + '</a>';
})
// or
// return text.replace(urlRegex, '<a href="$1">$1</a>')
}
var text = "Find me at http://www.example.com and also at http://stackoverflow.com";
var html = urlify(text);
// html after:
// "Find me at <a href="http://www.example.com">http://www.example.com</a> and also at <a href="http://stackoverflow.com">http://stackoverflow.com</a>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment