Skip to content

Instantly share code, notes, and snippets.

@smitroshin
Created November 5, 2018 12:58
Show Gist options
  • Save smitroshin/2e2b933d36f422b1247fb92d71aa65d9 to your computer and use it in GitHub Desktop.
Save smitroshin/2e2b933d36f422b1247fb92d71aa65d9 to your computer and use it in GitHub Desktop.
urlify
function urlify(text) {
var urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, function(url) {
return '<a href="' + url + '">' + url + '</a>';
})
}
var text = "Find me at http://www.example.com and also at http://stackoverflow.com";
var html = urlify(text);
// html now looks like:
// "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