Skip to content

Instantly share code, notes, and snippets.

@Aymkdn
Created June 5, 2018 10:08
Show Gist options
  • Save Aymkdn/4bfe85186f5dcf14b058048b23ae2e1e to your computer and use it in GitHub Desktop.
Save Aymkdn/4bfe85186f5dcf14b058048b23ae2e1e to your computer and use it in GitHub Desktop.
Parse URL in a string and convert in a link
var urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
function linkify(text, safeText) {
safeText = (safeText===false? false : true);
if (safeText) text=escapeHtml(text);
return text.replace(urlRegex, function(url) {
return '<a href="' + url + '">' + url + '</a>';
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment