Given a string with URLs in the following formats:
/
/
.html
/?hpt=ed_Intl
W JS/jQuery, how can I extract from the string just cnn for all of them? Top level domain plus extension?
Thanks
Given a string with URLs in the following formats:
https://www.cnn./
http://www.cnn./
http://www.cnn./2012/02/16/world/american-nicaragua-prison/index.html
http://edition.cnn./?hpt=ed_Intl
W JS/jQuery, how can I extract from the string just cnn. for all of them? Top level domain plus extension?
Thanks
Share Improve this question asked Feb 20, 2012 at 0:17 AnApprenticeAnApprentice 111k202 gold badges637 silver badges1k bronze badges 3- 2 url.match(/:\/\/(.[^/]+)/)[1]; – AnApprentice Commented Feb 20, 2012 at 0:23
- Why bother with regex. It's a Swiss army knife when all you need is a spoon. – tkone Commented Feb 20, 2012 at 0:26
-
@AnApprentice if you want subdomains, just use
location.host
. No need for regex at alla. – tkone Commented Feb 20, 2012 at 0:31
5 Answers
Reset to default 3var loc = document.createElement('a');
loc.href = 'http://www.cnn./2012/02/16/world/index.html';
window.alert(loc.hostname); // alerts "cnn."
Credits for the previous method:
Creating a new Location object in javascript
function domain(input){
var matches,
output = "",
urls = /\w+:\/\/([\w|\.]+)/;
matches = urls.exec(input);
if(matches !== null){
output = matches[1];
}
return output;
}
Given that there are top-level domains with dots in them, for example "co.uk", there's no way to do this programatically unless you include a list of all of the TLDs with dots in them.
var domain = location.host.split('.').slice(-2);
If you want it reassembled:
var domain = location.host.split('.').slice(-2).join('.');
But this won't work with co.uk or something. There's no hard nor fast rule for this, not even regex will determine that.
// something.domain. -> domain.
function getDomain() {
return window.location.hostname.replace(/([a-z]+.)/,"");
}