var domain = location.host.split('.').slice(-2);
If you want it to be reassembled:
var domain = location.host.split('.').slice(-2).join('.');
But that will not work with co.uk or anything else. There is no hard or fast rule for this; even a regular expression will not determine this.
source
share