I have domain name for eq.
1) http://www.abc.com/search
2) http://go.abc.com/work
I get only domain name from the above URL
Output
You can use a trick, by creating a -element, then setting the string to the href of that -element and then you have a Location object you can get the hostname from.
You could either add a method to the String prototype:
String.prototype.toLocation = function() {
var a = document.createElement('a');
a.href = this;
return a;
};
and use it like this:
"http://www.abc.com/search".toLocation().hostname
or make it a function:
function toLocation(url) {
var a = document.createElement('a');
a.href = url;
return a;
};
and use it like this:
toLocation("http://www.abc.com/search").hostname
both of these will output: "www.abc.com"
If you also need the protocol, you can do something like this:
var url = "http://www.abc.com/search".toLocation();
url.protocol + "//" + url.hostname
which will output: "http://www.abc.com"