-3

I want to extract just the subdomain part from the url. How to solve this problem. Eg. if the url is "http://www.example.com" how to get "www"?

1 Answers1

0

I would use URL() to extract the hostname of the url, then split() to extract the domain segments and get first one:

var url = new URL("http://www.example.com");
var subdomain = url.hostname.split('.')[0];
Allan Wind
  • 11,844
  • 4
  • 24
  • 32