获取没有扩展名的根域名 - Javascript

时间:2017-01-12 03:17:50

标签: javascript string parsing url

如何在没有扩展名的情况下从javascript中获取给定网址的根域名?

示例:

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

主要问题是我在没有扩展或子域的情况下创建解决方案时遇到了问题。

如何才能解决这个问题?

2 个答案:

答案 0 :(得分:0)

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迭代和解析/剥离子串方法。

答案 1 :(得分:0)

尝试:

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

另请参阅:Extract hostname name from string