Created
February 22, 2013 07:29
-
-
Save threetreeslight/5011486 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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