Skip to content

Instantly share code, notes, and snippets.

@turivishal
Created November 11, 2020 05:48
Show Gist options
  • Save turivishal/d8c9d796e47992b1f97a87abd93d67af to your computer and use it in GitHub Desktop.
Save turivishal/d8c9d796e47992b1f97a87abd93d67af to your computer and use it in GitHub Desktop.
Detect URL's from text in Javascript
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 https://www.example.com and also at http://stackoverflow.com';
var html = urlify(text);
console.log(html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment