0

I was wondering if there was a way to get the website name from the URL, using either JavaScript, or Node-JS.

Here is an example:

https://www.google.com     → google
http://www.facebook.com.au → facebook
duckduckgo.com             → duckduckgo
material.io                → material

I was thinking about filtering out common things like .com, www. etc, but then there are country codes, like .au, .cn.

  • 7
    Possible duplicate of [Extract hostname name from string](https://stackoverflow.com/questions/8498592/extract-hostname-name-from-string) – David R Jan 17 '18 at 07:13
  • 1
    Welcome to SO, you question seems opinion based, and I am afraid it will be closed by moderator. Please post some code what you have tried so far. – Nilesh Mahajan Jan 17 '18 at 07:14
  • Or duplicate of https://stackoverflow.com/questions/25942552/get-second-level-domain-name-from-url also has a good solution – scipper Jan 17 '18 at 07:15
  • Pretty much answers it :) Is there any place to find all of the TLDs though? – Jacob Collins Jan 17 '18 at 07:41

1 Answers1

0

Please find the solution,

function fetch_domain(url) { var str = url; var n = str.indexOf("://"); if(n>-1){ n = n+"://".length; }else{ n=0; } var x = str.indexOf("/www."); if(x>-1){ str = str.replace("/www.", "/"); } return str.slice(n, str.indexOf(".")); }

Thanks!

Amulya Kashyap
  • 2,333
  • 17
  • 25