如何在没有扩展名的情况下从javascript中的给定URL获取根域名?
示例:
rootDomain("https://www.etsy.com/market/tumblr_clothing") -> etsy
rootDomain("https://petitions.whitehouse.gov/") -> whitehouse
rootDomain("facebook.com") -> facebook
rootDomain("google.ca") -> google
我找到了一个不完整的解决方案:How to get domain name only using javascript?和Extract hostname name from string
主要的问题是我很难创建一个没有扩展或子域的解决方案。
如何才是解决这一问题的最佳方法?
发布于 2017-01-15 04:22:34
function rootDomain(url) {
var rightPeriodIndex;
for (var i = url.length - 1; i >= 0; i--) {
if(url[i] == '.') {
//console.log("rightPeriodIndex", i);
rightPeriodIndex = i;
var noExtension = url.substr(0,i);
console.log("this is noExtension", noExtension);
break;
}
}
var result = noExtension.substring(noExtension.lastIndexOf(".") + 1);
return result;
}
console.log(rootDomain("facebook.com"));
console.log(rootDomain("https://www.etsy.com/market/tumblr_clothing"));
console.log(rootDomain("https://petitions.whitehouse.gov/"));
console.log(rootDomain("google.ca"));最后,我使用javascript进行迭代和解析/剥离子字符串方法。
发布于 2018-11-07 21:06:12
尝试:
function extractHostname(url) {
var hostname;
if (url.indexOf("//") > -1) {
hostname = url.split('/')[2];
}
else {
hostname = url.split('/')[0];
}
hostname = hostname.split(':')[0];
hostname = hostname.split('?')[0];
return hostname;
}
function extractRootDomainNoExt(url) {
var domain = extractHostname(url),
splitArr = domain.split('.'),
arrLen = splitArr.length;
if (arrLen == 2) {
domain = splitArr[0]
}
else if (arrLen > 2) {
domain = splitArr[arrLen - 2];
//check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk")
if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) {
domain = splitArr[arrLen - 3];
}
}
return domain;
}
//test extractRootDomainNoExt
console.log("== Testing extractRootDomainNoExt: ==");
console.log(extractRootDomainNoExt("https://www.etsy.com/market/tumblr_clothing"));
console.log(extractRootDomainNoExt("https://petitions.whitehouse.gov/"));
console.log(extractRootDomainNoExt("facebook.com"));
console.log(extractRootDomainNoExt("google.ca"));
console.log(extractRootDomainNoExt("http://www.blog.classroom.me.uk/index.php"));
console.log(extractRootDomainNoExt("http://www.youtube.com/watch?v=ClkQA2Lb_iE"));
console.log(extractRootDomainNoExt("https://www.youtube.com/watch?v=ClkQA2Lb_iE"));
console.log(extractRootDomainNoExt("www.youtube.com/watch?v=ClkQA2Lb_iE"));
console.log(extractRootDomainNoExt("ftps://ftp.websitename.com/dir/file.txt"));
console.log(extractRootDomainNoExt("doesnothaveext/dir/file.txt"));
console.log(extractRootDomainNoExt("websitename.com:1234/dir/file.txt"));
console.log(extractRootDomainNoExt("ftps://websitename.com:1234/dir/file.txt"));
console.log(extractRootDomainNoExt("example.com?param=value"));
console.log(extractRootDomainNoExt("https://facebook.github.io/jest/"));
console.log(extractRootDomainNoExt("//youtube.com/watch?v=ClkQA2Lb_iE"));
*点击“运行代码片段”以查看各种用例中的工作。
这也适用于提取子域和国家编码的域名,如classroom.co.uk -> classroom。
https://stackoverflow.com/questions/41604540
复制相似问题