Skip to content

Instantly share code, notes, and snippets.

@wesinator
Forked from MichaelPaulukonis/linkify.js
Created February 20, 2024 06:14
Show Gist options
  • Save wesinator/d5ab7bc3da2c5603d18f5a3ec4aaa198 to your computer and use it in GitHub Desktop.
Save wesinator/d5ab7bc3da2c5603d18f5a3ec4aaa198 to your computer and use it in GitHub Desktop.
simple linkify (from MS Visual Studio upgrade report). See also: http://benalman.com/code/projects/javascript-linkify/examples/linkify/
// Linkifies the specified text content, replaces candidate links with html links
function linkify(text)
{
if(!text || 0 === text.length)
{
return text;
}
// Find http, https and ftp links and replace them with hyper links
var urlLink = /(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\/\\\+&%\$#\=~;\{\}])*/gi;
return text.replace(urlLink, '<a href="$&">$&</a>') ;
}
// Linkifies the specified element by ID
function linkifyElement(id)
{
var element = document.getElementById(id);
if(!!element)
{
element.innerHTML = linkify(element.innerHTML);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment