var host = location.hostname.replace( /www\./g, '' );
The 'g' flag is for 'global', which is necessary if you want to get a true "gsub" (all matches are replaced, not just the first ones).
It is better, however, to get the full TLD:
var tld = location.hostname.replace( /^(.+\.)?(\w+\.\w+)$/, '$2' );
This will process domains like foo.bar.jim.jam.com and give you just jam.com .
source share