13

I want to get domain name only using javascript. Ex

vn.search.yahoo.com -> yahoo
vn.search.yahoo.com.vn -> yahoo
sub1.sub2.sub3.abcdef.co.uk -> abcdef

Thank you!

Edit: "domain" = domain without extension (ex: .com, .net, .co.uk...) and without sub domain (ex: www, email, cdn, support...)

vNext
  • 1,102
  • 4
  • 16
  • 28

12 Answers12

33

Use location.host and cut off subdomains and the TLD:

 var domain = (location.host.match(/([^.]+)\.\w{2,3}(?:\.\w{2})?$/) || [])[1]

update: as @demix pointed out, this fails for 2 and 3-letter domains. It also won't work for domains like aero, jobs and dozens others.

The only way around is to know valid TLDs in advance, so here is a more appropriate function:

// http://data.iana.org/TLD/tlds-alpha-by-domain.txt
var TLDs = ["ac", "ad", "ae", "aero", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "arpa", "as", "asia", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "biz", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cat", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "com", "coop", "cr", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "edu", "ee", "eg", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gov", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "info", "int", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jobs", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mil", "mk", "ml", "mm", "mn", "mo", "mobi", "mp", "mq", "mr", "ms", "mt", "mu", "museum", "mv", "mw", "mx", "my", "mz", "na", "name", "nc", "ne", "net", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "org", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "pro", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "su", "sv", "sy", "sz", "tc", "td", "tel", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "travel", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "xn--0zwm56d", "xn--11b5bs3a9aj6g", "xn--3e0b707e", "xn--45brj9c", "xn--80akhbyknj4f", "xn--90a3ac", "xn--9t4b11yi5a", "xn--clchc0ea0b2g2a9gcd", "xn--deba0ad", "xn--fiqs8s", "xn--fiqz9s", "xn--fpcrj9c3d", "xn--fzc2c9e2c", "xn--g6w251d", "xn--gecrj9c", "xn--h2brj9c", "xn--hgbk6aj7f53bba", "xn--hlcj6aya9esc7a", "xn--j6w193g", "xn--jxalpdlp", "xn--kgbechtv", "xn--kprw13d", "xn--kpry57d", "xn--lgbbat1ad8j", "xn--mgbaam7a8h", "xn--mgbayh7gpa", "xn--mgbbh1a71e", "xn--mgbc0a9azcg", "xn--mgberp4a5d4ar", "xn--o3cw4h", "xn--ogbpf8fl", "xn--p1ai", "xn--pgbs0dh", "xn--s9brj9c", "xn--wgbh1c", "xn--wgbl6a", "xn--xkc2al3hye2a", "xn--xkc2dl3a5ee0h", "xn--yfro4i67o", "xn--ygbi2ammx", "xn--zckzah", "xxx", "ye", "yt", "za", "zm", "zw"].join()

function getDomain(url){

    var parts = url.split('.');
    if (parts[0] === 'www' && parts[1] !== 'com'){
        parts.shift()
    }
    var ln = parts.length
      , i = ln
      , minLength = parts[parts.length-1].length
      , part

    // iterate backwards
    while(part = parts[--i]){
        // stop when we find a non-TLD part
        if (i === 0                    // 'asia.com' (last remaining must be the SLD)
            || i < ln-2                // TLDs only span 2 levels
            || part.length < minLength // 'www.cn.com' (valid TLD as second-level domain)
            || TLDs.indexOf(part) < 0  // officialy not a TLD
        ){
            return part
        }
    }
}

getDomain(location.host)

I hope I didn't miss too many corner cases. This should be available in the location object :(

Test cases: http://jsfiddle.net/hqBKd/4/

A list of TLDs can be found here: http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1

errows
  • 3
  • 1
Ricardo Tomasi
  • 34,573
  • 2
  • 55
  • 66
  • @demix oops. would have to validate the TLD :/ – Ricardo Tomasi Nov 24 '11 at 07:05
  • This will work in examples given in question but as demix said will fail on domain with name length of 2 and 3, specially URL shortening domain fall in this category. – Gagandeep Singh Nov 24 '11 at 07:10
  • 1
    Some real world "corner cases" that weren't too hard to spot: `www.epa.qld.gov.au` returns "qld" when "epa" might be expected. `www.edu.com` returns "www" where "edu" might be expected. The OP has yet to explain what a "domain" is. – RobG Nov 24 '11 at 09:43
  • "qld" is the second-level domain in that case: www.qld.gov.au, `qld` is not part of a top-level domain. Fixed `www.edu.com`. I don't know of anyone that provides a full list of countr-code second-level domains.. – Ricardo Tomasi Nov 24 '11 at 17:57
  • 1
    I'd suggest using a library like URI.js for this (http://medialize.github.com/URI.js/). It is robust and has decent community. – Georgii Ivankin Mar 21 '13 at 17:05
  • I don't understand why you need a list of country TLDs. If it ends in 2 letters, it's a country. The difficulty is determining which countries properly divide the remainder of their domain. e.g. in the above example "www.qld.gov.au" "qld" is *not* the second-level domain, anymore that "www.whitehouse.gov." since there only about 4 countries who haven't sold their TLD rights, you need only check for those (remove two levels) and remove 1 level for the rest. At a guess, the only two level domains are: .au, .uk, .nz, .ca (comment if i've missed any). http://publicsuffix.org/ has a ful list – Orwellophile Feb 24 '14 at 05:54
  • @Orwellophile if you read this answer again, you'll see that was the initial approach. `xxx.uk`, `xxx.co.uk` are both valid, and in both `xxx` is the "domain name". On the other hand, `co.uk` is not a valid address, while `be.at` could be. There is no way to account for these differences other than looking at the list of registered TLDs and SLDs. – Ricardo Tomasi Mar 07 '14 at 21:08
  • when you use `location.host`, the port number is also included, (if specified) in contrast to `window.location.hostname`, see this reference: http://bl.ocks.org/abernier/3070589 – Daan Jan 03 '17 at 13:39
11

I was looking for something that would work for the majority of cases, without having to maintain the TLD list (and skip it's size!). It seems to me that you can do this pretty accurately by looking instead at the Second-Level Domain for common ones:

function getDomainName(domain) {
    var parts = domain.split('.').reverse();
    var cnt = parts.length;
    if (cnt >= 3) {
        // see if the second level domain is a common SLD.
        if (parts[1].match(/^(com|edu|gov|net|mil|org|nom|co|name|info|biz)$/i)) {
            return parts[2] + '.' + parts[1] + '.' + parts[0];
        }
    }
    return parts[1]+'.'+parts[0];
}

Fiddle & Tests @ http://jsfiddle.net/mZPaf/2/

Critiques/thoughts welcome.

AcidPAT
  • 709
  • 7
  • 13
2
var docdomain = document.domain.split('.');
var dom1 = "";
if (typeof (docdomain[docdomain.length - 2]) != 'undefined') dom1 = docdomain[docdomain.length - 2] + '.';
var domain = dom1 + docdomain[docdomain.length - 1];
console.log(domain);

//without subdomains
j0k
  • 22,600
  • 28
  • 79
  • 90
icke
  • 21
  • 1
1

Without having a complete list of TLD's (which would get very long). If you just need the domain name from the current page you can use my technique (using cookies to find the root domain)

Javascript - Get Domain Name Excluding Subdomain

To remove the extension you can then use the first element from a str.split('.')[0]

DIF
  • 2,470
  • 6
  • 35
  • 49
Ross
  • 11
  • 1
  • This is excellent solution which covers a lot of corner cases. Can you format your answer with code example and addition to answer question? After that this answer can be accepted. – terales Jun 11 '17 at 06:57
  • Also it lacks some explanation that it may not work for scripts embedded with iframe: https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy – terales Jun 11 '17 at 07:01
  • I know this is an old answer but you should include the code here (in the answer) in case the linked site is unavailable for some reason in the future - in which case this answer becomes useless. – naththedeveloper Oct 16 '18 at 12:40
1

The only way I can imagine is list all the TLD. Sample code like below.

function getDomainName(){
    var domainList = ['com','org','net',...];//all  TLD
    var tokens = document.domain.split('.');
    while(tokens.length){
        var token = tokens.pop();
        if( domainList.indexOf(token) == -1 ){
            return token;
        }
    }
    return null;
}

Array.prototype.indexOf should do some fix in IE.

demix
  • 76
  • 7
0

i needed to do this and whipped up something simple that accounted for my use case

function stripSubDomainAndTLD (domain) {
    return domain.replace(/^(?:[a-z0-9\-\.]+\.)??([a-z0-9\-]+)(?:\.com|\.net|\.org|\.biz|\.ws|\.in|\.me|\.co\.uk|\.co|\.org\.uk|\.ltd\.uk|\.plc\.uk|\.me\.uk|\.edu|\.mil|\.br\.com|\.cn\.com|\.eu\.com|\.hu\.com|\.no\.com|\.qc\.com|\.sa\.com|\.se\.com|\.se\.net|\.us\.com|\.uy\.com|\.ac|\.co\.ac|\.gv\.ac|\.or\.ac|\.ac\.ac|\.af|\.am|\.as|\.at|\.ac\.at|\.co\.at|\.gv\.at|\.or\.at|\.asn\.au|\.com\.au|\.edu\.au|\.org\.au|\.net\.au|\.id\.au|\.be|\.ac\.be|\.adm\.br|\.adv\.br|\.am\.br|\.arq\.br|\.art\.br|\.bio\.br|\.cng\.br|\.cnt\.br|\.com\.br|\.ecn\.br|\.eng\.br|\.esp\.br|\.etc\.br|\.eti\.br|\.fm\.br|\.fot\.br|\.fst\.br|\.g12\.br|\.gov\.br|\.ind\.br|\.inf\.br|\.jor\.br|\.lel\.br|\.med\.br|\.mil\.br|\.net\.br|\.nom\.br|\.ntr\.br|\.odo\.br|\.org\.br|\.ppg\.br|\.pro\.br|\.psc\.br|\.psi\.br|\.rec\.br|\.slg\.br|\.tmp\.br|\.tur\.br|\.tv\.br|\.vet\.br|\.zlg\.br|\.br|\.ab\.ca|\.bc\.ca|\.mb\.ca|\.nb\.ca|\.nf\.ca|\.ns\.ca|\.nt\.ca|\.on\.ca|\.pe\.ca|\.qc\.ca|\.sk\.ca|\.yk\.ca|\.ca|\.cc|\.ac\.cn|\.com\.cn|\.edu\.cn|\.gov\.cn|\.org\.cn|\.bj\.cn|\.sh\.cn|\.tj\.cn|\.cq\.cn|\.he\.cn|\.nm\.cn|\.ln\.cn|\.jl\.cn|\.hl\.cn|\.js\.cn|\.zj\.cn|\.ah\.cn|\.gd\.cn|\.gx\.cn|\.hi\.cn|\.sc\.cn|\.gz\.cn|\.yn\.cn|\.xz\.cn|\.sn\.cn|\.gs\.cn|\.qh\.cn|\.nx\.cn|\.xj\.cn|\.tw\.cn|\.hk\.cn|\.mo\.cn|\.cn|\.cx|\.cz|\.de|\.dk|\.fo|\.com\.ec|\.tm\.fr|\.com\.fr|\.asso\.fr|\.presse\.fr|\.fr|\.gf|\.gs|\.co\.il|\.net\.il|\.ac\.il|\.k12\.il|\.gov\.il|\.muni\.il|\.ac\.in|\.co\.in|\.org\.in|\.ernet\.in|\.gov\.in|\.net\.in|\.res\.in|\.is|\.it|\.ac\.jp|\.co\.jp|\.go\.jp|\.or\.jp|\.ne\.jp|\.ac\.kr|\.co\.kr|\.go\.kr|\.ne\.kr|\.nm\.kr|\.or\.kr|\.li|\.lt|\.lu|\.asso\.mc|\.tm\.mc|\.com\.mm|\.org\.mm|\.net\.mm|\.edu\.mm|\.gov\.mm|\.ms|\.nl|\.no|\.nu|\.pl|\.ro|\.org\.ro|\.store\.ro|\.tm\.ro|\.firm\.ro|\.www\.ro|\.arts\.ro|\.rec\.ro|\.info\.ro|\.nom\.ro|\.nt\.ro|\.se|\.si|\.com\.sg|\.org\.sg|\.net\.sg|\.gov\.sg|\.sk|\.st|\.tf|\.ac\.th|\.co\.th|\.go\.th|\.mi\.th|\.net\.th|\.or\.th|\.tm|\.to|\.com\.tr|\.edu\.tr|\.gov\.tr|\.k12\.tr|\.net\.tr|\.org\.tr|\.com\.tw|\.org\.tw|\.net\.tw|\.ac\.uk|\.uk\.com|\.uk\.net|\.gb\.com|\.gb\.net|\.vg|\.sh|\.kz|\.ch|\.info|\.ua|\.gov|\.name|\.pro|\.ie|\.hk|\.com\.hk|\.org\.hk|\.net\.hk|\.edu\.hk|\.us|\.tk|\.cd|\.by|\.ad|\.lv|\.eu\.lv|\.bz|\.es|\.jp|\.cl|\.ag|\.mobi|\.eu|\.co\.nz|\.org\.nz|\.net\.nz|\.maori\.nz|\.iwi\.nz|\.io|\.la|\.md|\.sc|\.sg|\.vc|\.tw|\.travel|\.my|\.se|\.tv|\.pt|\.com\.pt|\.edu\.pt|\.asia|\.fi|\.com\.ve|\.net\.ve|\.fi|\.org\.ve|\.web\.ve|\.info\.ve|\.co\.ve|\.tel|\.im|\.gr|\.ru|\.net\.ru|\.org\.ru|\.hr|\.com\.hr)$/, '$1');
}

mainly i just wanted to remove all subdomains, unfortunately this isn't 100% for some of the new TLD's but it works pretty well, and you can always add to the regex.

http://jsfiddle.net/icodeforlove/TzjJE/2/

Chad Cache
  • 9,668
  • 3
  • 56
  • 48
0

With the help of other friend's code examples given above I created a function which will return only domain name and if it is not valid domain for example TLD is missing then it will attach ".com" as per my requirement.

function getDomain(url){

var TLDs = ["ac", "ad", "ae", "aero", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "arpa", "as", "asia", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "biz", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cat", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "com", "coop", "cr", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "edu", "ee", "eg", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gov", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "info", "int", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jobs", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mil", "mk", "ml", "mm", "mn", "mo", "mobi", "mp", "mq", "mr", "ms", "mt", "mu", "museum", "mv", "mw", "mx", "my", "mz", "na", "name", "nc", "ne", "net", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "org", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "pro", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "su", "sv", "sy", "sz", "tc", "td", "tel", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "travel", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "xn--0zwm56d", "xn--11b5bs3a9aj6g", "xn--3e0b707e", "xn--45brj9c", "xn--80akhbyknj4f", "xn--90a3ac", "xn--9t4b11yi5a", "xn--clchc0ea0b2g2a9gcd", "xn--deba0ad", "xn--fiqs8s", "xn--fiqz9s", "xn--fpcrj9c3d", "xn--fzc2c9e2c", "xn--g6w251d", "xn--gecrj9c", "xn--h2brj9c", "xn--hgbk6aj7f53bba", "xn--hlcj6aya9esc7a", "xn--j6w193g", "xn--jxalpdlp", "xn--kgbechtv", "xn--kprw13d", "xn--kpry57d", "xn--lgbbat1ad8j", "xn--mgbaam7a8h", "xn--mgbayh7gpa", "xn--mgbbh1a71e", "xn--mgbc0a9azcg", "xn--mgberp4a5d4ar", "xn--o3cw4h", "xn--ogbpf8fl", "xn--p1ai", "xn--pgbs0dh", "xn--s9brj9c", "xn--wgbh1c", "xn--wgbl6a", "xn--xkc2al3hye2a", "xn--xkc2dl3a5ee0h", "xn--yfro4i67o", "xn--ygbi2ammx", "xn--zckzah", "xxx", "ye", "yt", "za", "zm", "zw"].join()

    url = url.replace(/.*?:\/\//g, "");
    url = url.replace(/www./g, "");
    var parts = url.split('/');
    url = parts[0];
    var parts = url.split('.');
    if (parts[0] === 'www' && parts[1] !== 'com'){
        parts.shift()
    }
    var ln = parts.length
      , i = ln
      , minLength = parts[parts.length-1].length
      , part

    // iterate backwards
    while(part = parts[--i]){
        // stop when we find a non-TLD part
        if (i === 0                    // 'asia.com' (last remaining must be the SLD)
            || i < ln-2                // TLDs only span 2 levels
            || part.length < minLength // 'www.cn.com' (valid TLD as second-level domain)
            || TLDs.indexOf(part) < 0  // officialy not a TLD
        ){
            var actual_domain = part;
            break;
            //return part
        }
    }
    //console.log(actual_domain);
    var tid ;
    if(typeof parts[ln-1] != 'undefined' && TLDs.indexOf(parts[ln-1]) >= 0)
    {
        tid = '.'+parts[ln-1];
    }
    if(typeof parts[ln-2] != 'undefined' && TLDs.indexOf(parts[ln-2]) >= 0)
    {
        tid = '.'+parts[ln-2]+tid;
    }
    if(typeof tid != 'undefined')
        actual_domain = actual_domain+tid;
    else
        actual_domain = actual_domain+'.com';


    return actual_domain;
}
0

You could use document.domain to determine the domain name of the current page.

When setting document.domain, an error is thrown if the new value is invalid. A plain eTLD is invalid, while an eTLD+1 is valid. The function below loops to find a valid value, then returns the domain name portion.

This is comprehensive, because the browser uses the Public Suffix List to validate new values.

function getDomainName() {
    const original = document.domain;
    const parts = location.hostname.split('.');
    let etld = parts.pop();
    while (parts.length) {
        const name = parts.pop();
        const test = name + '.' + etld;
        try {
            document.domain = test;
            // we found the eTLD+1
            
            // reset before returning
            document.domain = original;

            return name;
        } catch (e) {
            // eTLDs and eTLD fragments fail
            etld = test;
        }
    }
}
kevlened
  • 10,846
  • 4
  • 23
  • 17
0

It's simple:

    var tokens = document.domain.split('.');
    var domain = tokens[tokens.length - 2];
ioseb
  • 16,625
  • 3
  • 33
  • 29
  • I tested with both and it works. At least in FF. Maybe some browser quirk? – ioseb Nov 24 '11 at 07:01
  • This will not work on domain with two level (like .com.vn & co.uk). For example it will not work on vn.search.yahoo.com.vn, it will give 'com' instead of 'yahoo'. see http://jsfiddle.net/gagan/SLeKQ/ – Gagandeep Singh Nov 24 '11 at 07:02
  • I'm using Chrome...did you see my link? – Purag Nov 24 '11 at 07:03
  • Got it. This won't work for such domains, though grabbing domain name and splitting it and retrieving actual name are two separate tasks. I think this can be used to farther refine "algorithm" IMHO. – ioseb Nov 24 '11 at 07:08
  • Domain names can be long: `www.epa.qld.gov.au` or short `stackoverflow.com` or anything in between. What are you calling the "domain" here? – RobG Nov 24 '11 at 07:14
  • According to question's author it's for example "bbc" from www.bbc.co.uk – ioseb Nov 24 '11 at 07:15
  • So what is the domain in `www.epa.qld.gov.au`? – RobG Nov 24 '11 at 07:20
  • I do not have idea. But does it matter? – ioseb Nov 24 '11 at 07:28
  • @RobG 'qld', simply because it's not registered as a top level domain. The definition of what he's after is http://en.wikipedia.org/wiki/Second-level_domain – Ricardo Tomasi Nov 24 '11 at 07:40
  • The point is that the OP hasn't defined what part of a domain they are after, despite being asked. There are generic comains (.com, .biz, .edu, etc.), country domains (.nz, .uk, .us, etc.) state or regional domains (.qld, .vic, etc.) and so on. Note also that SLDs change depending on the TLD - there is ac.uk or edu.au, but what do you do with edu.biz or ac.com? – RobG Nov 24 '11 at 09:28
-1

What about this?

    function getDomain(){
        if(document.domain.length){
            var parts = document.domain.replace(/^(www\.)/,"").split('.');

            //is there a subdomain? 
            while(parts.length > 2){
                //removing it from our array 
                var subdomain = parts.shift();
            }

            //getting the remaining 2 elements
            var domain = parts.join('.');

            return domain.replace(/(^\.*)|(\.*$)/g, "");
        }
        return '';
    }
Alvaro
  • 40,778
  • 30
  • 164
  • 336
-1

RegEx I use to get domain name only: ([^.]*[.]){0,}([^.]*)(\.[^.]*) The domain can be found in the second part.

Marvin Fischer
  • 2,552
  • 3
  • 23
  • 34
-2
function getDomainName( hostname ) {
    var TLDs = new RegExp(/\.(com|net|org|biz|ltd|plc|edu|mil|asn|adm|adv|arq|art|bio|cng|cnt|ecn|eng|esp|etc|eti|fot|fst|g12|ind|inf|jor|lel|med|nom|ntr|odo|ppg|pro|psc|psi|rec|slg|tmp|tur|vet|zlg|asso|presse|k12|gov|muni|ernet|res|store|firm|arts|info|mobi|maori|iwi|travel|asia|web|tel)(\.[a-z]{2,3})?$|(\.[^\.]{2,3})(\.[^\.]{2,3})$|(\.[^\.]{2})$/);
    return hostname.replace(TLDs, '').split('.').pop();
}

/* TEST */

var domains = [
    'domain.com',
    'subdomain.domain.com',
    'www.subdomain.domain.com',
    'www.subdomain.domain.info',
    'www.subdomain.domain.info.xx',
    'mail.subdomain.domain.co.uk',
    'mail.subdomain.domain.xxx.yy',
    'mail.subdomain.domain.xx.yyy',
    'mail.subdomain.domain.xx',
    'domain.xx'
];

var result = [];
for (var i = 0; i < domains.length; i++) {
    result.push( getDomainName( domains[i] ) );
}

alert ( result.join(' | ') );
// result: domain | domain | domain | domain | domain | domain | domain | domain | domain | domain
PHC
  • 67
  • 1