Created
October 10, 2013 14:04
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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