How i can get the domain name example.com
from the set of possible subdomains sub1.example.com
sub2.example.com
sub3.example.com
using javascript ...?
Firstly, we'd need to extract the host from the given URL value. We can use the URI class: String urlString = "https://www.baeldung.com/java-tutorial"; URI uri = new URI(urlString); String host = uri. getHost();
var parts = location.hostname.split('.'); var subdomain = parts.shift(); var upperleveldomain = parts.join('.');
To get only the second-level-domain, you might use
var parts = location.hostname.split('.'); var sndleveldomain = parts.slice(-2).join('.');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With