JSFiddle - React, Tailwind, and code Playground
HTML
<!-- Show the console to see output -->
JavaScript
var addresses = [
'google.com', // should return 'com'
'https://google.com.uk', // should return 'uk'
'yahoo.cn/foo/bar.foo', // should return 'cn'
'file:///usr/local' // should fail
];
for (var index in addresses) {
console.log(tld(addresses[index]));
}
function tld(address) {
// handle edge-cases
if (typeof address == 'undefined' || address.indexOf('file:///') != -1)
return undefined;
var part = address;
//remove http://
if (part.indexOf('//') != -1)
part = part.split('//')[1];
//isolate domain
if (part.indexOf('/') != -1)
part = part.split('/')[0];
//get tld
if (part.indexOf('.') != -1) {
var all = part.split('.');
part = all[all.length - 1];
}
return part;
}