Skip to content

Instantly share code, notes, and snippets.

@MichaelPaulukonis
Created October 10, 2013 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MichaelPaulukonis/6918920 to your computer and use it in GitHub Desktop.
Save MichaelPaulukonis/6918920 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