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"?
Asked
Active
Viewed 183 times
-3
-
try using js split. – Sagar Oct 26 '21 at 05:11
-
var location = window.location; console.log(location); – Ravi Ashara Oct 26 '21 at 05:15
1 Answers
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
- 23,068
- 5
- 28
- 38