2

I was trying out this from a very long time, Need a JavaScript function to extract Parent domain from URL.

Input
https://app.domainname.io
http://domainname.net
https://domainname.com
https://app.domain.com
https://www.google.co.in
http://dev2-aa.domain-name.com
https://app.domain.co.in

Output
domainname.io
domainname.net
domainname.com
domain.com
google.co.in
domain-name.com
domain.co.in

Thank you

2 Answers2

1

This solution might not be perfect but works for your sample data:

function extractDomain(url) {
  return url.match(/https?:\/\/(?:\S+\.)*(\S{3,}(?:\.\S{1,3}){1,2})/)[1]
}

Explanation:

https?:\/\/: Looks for http or https

(?:\S+\.)*: Matches but not captures any number of subdomains

(\S{3,}(?:\.\S{1,3}){1,2}): Captures the domain and the TLD. In detail:

S{3,}: Looks for something longer which should be the domain part (eg. google). This is not perfect, because a 2 letters long domain name would not match.

(?:\.\S{1,3}){1,2}: Matches the TLD part: a single TLD (eg. .com) or two short parts (eg. .co.in).

ajuhos
  • 111
  • 1
  • 6
0

We could try this,

function getHostName(url) {
    var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
    if (match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) {
    return match[2];
    }
    else {
        return null;
    }
}

function getDomain(url) {
    var hostName = getHostName(url);
    var domain = hostName;

    if (hostName != null) {
        var parts = hostName.split('.').reverse();

        if (parts != null && parts.length > 1) {
            domain = parts[1] + '.' + parts[0];

            if (hostName.toLowerCase().indexOf('.co.uk') != -1 && parts.length > 2) {
              domain = parts[2] + '.' + domain;
            }
        }
    }

    return domain;
}
Liju Kuriakose
  • 445
  • 3
  • 11