给定一个包含以下格式的URL的字符串:
https://www.cnn.com/
http://www.cnn.com/
http://www.cnn.com/2012/02/16/world/american-nicaragua-prison/index.html
http://edition.cnn.com/?hpt=ed_IntlW/jQuery,我怎么才能从字符串中提取出它们的cnn.com呢?顶级域名加扩展?
谢谢
发布于 2012-02-20 08:33:32
var loc = document.createElement('a');
loc.href = 'http://www.cnn.com/2012/02/16/world/index.html';
window.alert(loc.hostname); // alerts "cnn.com"前一种方法的积分:
发布于 2016-02-26 06:25:34
function domain(input){
var matches,
output = "",
urls = /\w+:\/\/([\w|\.]+)/;
matches = urls.exec(input);
if(matches !== null){
output = matches[1];
}
return output;
}发布于 2012-02-20 08:24:50
var domain = location.host.split('.').slice(-2);如果你想重新组装它:
var domain = location.host.split('.').slice(-2).join('.');但这对co.uk或其他东西不起作用。对此没有硬性或快捷性规则,甚至正则表达式也无法确定这一点。
https://stackoverflow.com/questions/9354709
复制相似问题