2

Is there a way to get a domain without any subdomain from an url with javascript? I tried some approaches, but all of them deliver domain with subdomain.

All regular expressions i was seeing were able just to filter www out - other subdomains were on their place.

Maybe somebody knows a javascript hint, like this one in Java.

Possible input: https://us.www.example.com/folder-1/index.html

Desired output: example.com

To go more in detail: i'm working on a Chrome extension and want to pass a domain from background.js to popup.js. I get current url, and then the domain from the current url with:

chrome.tabs.onUpdated.addListener(function(tabid, changeInfo, tab){
    chrome.tabs.query({'active' : true, 'currentWindow': true}, function(tabs){
        let newUrl = new URL(tabs[0].url);
        currentDomain = newUrl.hostname;
});

But - trouble - currentDomain comes with subdomain.

Evgeniy
  • 2,337
  • 2
  • 28
  • 68

0 Answers0