Created
July 2, 2015 08:25
-
-
Save mathdroid/c5e51a3784dcafd4d16c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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