I try to get only the domain name i.e. google.com from javascript
document.location.hostname
This code returns www.google.com.
How can I only get google.com? In this case it would be to either remove the www. or get only the domain name (if there’s such a method in javascript).
The ‘g’ flag is for ‘global’, which is needed if you want a true “gsub” (all matches replaced, not just the first).
Better, though, would be to get the full TLD:
This will handle domains like
foo.bar.jim.jam.comand give you justjam.com.