Skip to content

Instantly share code, notes, and snippets.

@threetreeslight
Created February 22, 2013 07:29
Show Gist options
  • Save threetreeslight/5011486 to your computer and use it in GitHub Desktop.
Save threetreeslight/5011486 to your computer and use it in GitHub Desktop.
// Detect links pattern
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
// Wrap the matched strings with `<span class="fake-link"></span>`
$('body').html($('body').html().replace(exp, "<span class='fake-link'>$1</span>"));
$('.fake-link').each(function() {
// Extra job: Check if parent is an anchor
if ($(this).parent().is('a')) {
// If `true`, then unwrap the original anchor which has been written by default from `.fake-link`
$(this).unwrap();
}
// Replace `.fake-link` with an anchor tag
$(this).replaceWith('<a href="' + $(this).text() + '">' + $(this).text() + '</a>');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment