Skip to content

Instantly share code, notes, and snippets.

@toddnestor
Created March 3, 2016 04:23
Show Gist options
  • Save toddnestor/99c99b16e05c683820b5 to your computer and use it in GitHub Desktop.
Save toddnestor/99c99b16e05c683820b5 to your computer and use it in GitHub Desktop.
<h2>Original: </h2>
<div id="original">http://0e24f87c32490a111f93f3579380e24d7f3579f.0e24f87c32490a1117f3579380e24f87c32490a111a3d7f3579f.0e24f87c32490a111f.0e24f87c32490a111f937ffffa3.0e24f82490af111f9f373d7f3579380e24f87c2490a111f937a3d7f3579f.tdneffsdsafstor.sometdhing.smarffffftmefsmbffff.com.uk/whaffdffffffffffffffffffsadffdsarg+whatev?he=that http://google.com/somewhere toddnestor.com yourmom.com google.com http://google.com somewhere.so whatever.sow.u whatever.sow.uk wow.wo https:something.com https://something.com //solid.online <img src="http://google.com" /> toddnestor.com, toddnestor.com. toddnestor.com: toddnestor.com? toddnestor.com!</div>
<h2>Linkified: </h2>
<div id="linkified"></div>
<script>
var linkify = function( string ) {
var re = /(?:^|\ )(((?:(?:http(?:s)?\:)?(?:\/\/))|(?:\/\/))?(?:(?=[a-z0-9\-\.]{1,255}(?=\/|\ |$|\:|\?|\,|\!))(?:(?:(?:[a-z0-9]{1}(?:[a-z0-9\-]{1,62})?\.){1,127})[a-z]{2,}(?:\.[a-z]{2})?))(?:[a-z0-9\/\-\_\%\?\&\!\$\'\,\(\)\*\+\=\;])*?)(?=$|\.(?=\ |$)|\:|\ |\?(?=\ |$)|\,|\!)/ig;
return string.replace(re, function( match, p1, p2 ){
return '<a href="' + ( p2 ? '' : '//' ) + p1 + '">' + p1 + '</a>';
});
}
document.getElementById('linkified').innerHTML = linkify( document.getElementById( 'original').innerHTML );
</script>
@toddnestor
Copy link
Author

This one follows the domain rules, it matches things that start with http://, https://, //, or none of those. It also will allow up to 127 levels of domain/subdomain/subsubdomain/etc, which is what is allowed. each section can only be up to 63 characters, contain only letters, numbers, or hyphens, but can't start with a hyphen. It will work with all tlds and country level tlds, including ones that don't exist yet. It matches tlds that are only letters, infinite length, if it has a country level one then that one has to be two characters. it can have a trailing slash or not, if it does it will include everything after that slash, I still need to update it so the url can't end with a comma.

It skips urls that are contained in quotes, so it won't mistakenly turn the urls that are hrefs or srcs into links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment