2

I am trying to create a javascript function that can extract only the 'domain' and 'top-level domain' from a url string.

The current questions on StackOverFlow do not resolve the answer for non-urls too.

Examples:

  1. https://www.google.com/imgres?imgurl -> google.com
  2. yahoo.com/mail -> yahoo.com
  3. http://helloworld.net/index/test/help -> helloworld.net
  4. www.stackoverflow.com/ -> stackoverflow.com
  5. invalid.url -> "return false or an empty string"

Any/All help is welcome and appreciated. Thank you.

Chadillac
  • 309
  • 5
  • 18
  • 3
    Unless wanting this for learning purposes, there is already something that will do that: [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL), though it does need to have the protocol part for it to be valid input, and will have to strip the subdomain parts (www) to get the output you seek – Patrick Evans Jan 15 '18 at 20:10
  • using the `window.location` as the parameter for the URL method – blubberbo Jan 15 '18 at 20:13
  • Possible duplicate of [How to get top level domain (base domain) from the url in javascript](https://stackoverflow.com/questions/6449340/how-to-get-top-level-domain-base-domain-from-the-url-in-javascript) – elbecita Jan 15 '18 at 20:15

2 Answers2

0

A regex can suit your needs, for example:

^(?:https?://)?(?:[^/]+\.)?([^./]+\.[^./]+).*$

See on Debuggex

In JS:

function extractDomain(url) {
  return url.replace(/^(?:https?:\/\/)?(?:[^\/]+\.)?([^.\/]+\.[^.\/]+).*$/, "$1");
}

If you want to handle TLDs that contain dots (like .co.uk), then I'm afraid the only solution is to hardcode them, for example:

^(?:https?://)?(?:[^/]+\.)?([^./]+\.(?:co\.uk|com|de|es|fr)).*$

See on Debuggex

sp00m
  • 47,968
  • 31
  • 142
  • 252
0

This is quite simple.

window.location.href variable stores the current url of the website.

With this variable, you can get the top domain.

var url = window.location.href
var split1 = url.split('//')[1];   // Get the string except the protocol.
var split2 = split1.split('/')[0];    // Get the domain url.
// Get top domain url.
var domain = split2;
if (split2.substring(0, 4) == 'www.')
    domain = split2.slice(4)

variable domain is the value you want.