JamesJameson2456
JamesJameson2456

Reputation: 13

Extract domain name from ANY type of url

There are many question on SO for extracting the domain name from a variety of URLs, but none of them cover ALL sub-domains and suffixes (.org, .uk, .com, .org.uk, .whatever etc).

For example:

http://uselesstext.moretext.DOMAIN.org.uk/

Result: DOMAIN.org.uk

The main problem here is that it's seemingly impossible to design an algorithm to identify which part of the URL is the relevant part (without the use of a library) because there can be a varying amount of sub-domains and suffixes.

Any ideas? (In JavaScript preferably)

Upvotes: 0

Views: 636

Answers (1)

user149341
user149341

Reputation:

What you are looking for here is the Public Suffix List. It's a list of every domain extension that can have independently administrated domains registered under it (e.g, com, org.uk, etc).

There are a number of Javascript implementations of this list available. The two most common ones are publicsuffixlist.js and tld.js.

Upvotes: 1

Related Questions