且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从字符串中提取主机名

更新时间:2023-02-23 17:30:16

尝试:

function extractHostname(url) {
    var hostname;
    //find & remove protocol (http, ftp, etc.) and get hostname

    if (url.indexOf("//") > -1) {
        hostname = url.split('/')[2];
    }
    else {
        hostname = url.split('/')[0];
    }

    //find & remove port number
    hostname = hostname.split(':')[0];
    //find & remove "?"
    hostname = hostname.split('?')[0];

    return hostname;
}

// To address those who want the "root domain," use this function:
function extractRootDomain(url) {
    var domain = extractHostname(url),
        splitArr = domain.split('.'),
        arrLen = splitArr.length;

    //extracting the root domain here
    //if there is a subdomain 
    if (arrLen > 2) {
        domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
        //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) {
            //this is using a ccTLD
            domain = splitArr[arrLen - 3] + '.' + domain;
        }
    }
    return domain;
}

//test the code
console.log("== Testing extractHostname: ==");
console.log(extractHostname("http://www.blog.clas-s-room.me.uk/index.php"));
console.log(extractHostname("http://www.***.com/watch?v=ClkQA2Lb_iE"));
console.log(extractHostname("https://www.***.com/watch?v=ClkQA2Lb_iE"));
console.log(extractHostname("www.***.com/watch?v=ClkQA2Lb_iE"));
console.log(extractHostname("ftps://ftp.websitename.com/dir/file.txt"));
console.log(extractHostname("websitename.com:1234/dir/file.txt"));
console.log(extractHostname("ftps://websitename.com:1234/dir/file.txt"));
console.log(extractHostname("example.com?param=value"));
console.log(extractHostname("https://facebook.github.io/jest/"));
console.log(extractHostname("//***.com/watch?v=ClkQA2Lb_iE"));
console.log(extractHostname("http://localhost:4200/watch?v=ClkQA2Lb_iE"));

//test extractRootDomain
console.log("== Testing extractRootDomain: ==");
console.log(extractRootDomain("http://www.blog.clas-s-room.me.uk/index.php"));
console.log(extractRootDomain("http://www.***.com/watch?v=ClkQA2Lb_iE"));
console.log(extractRootDomain("https://www.***.com/watch?v=ClkQA2Lb_iE"));
console.log(extractRootDomain("www.***.com/watch?v=ClkQA2Lb_iE"));
console.log(extractRootDomain("ftps://ftp.websitename.com/dir/file.txt"));
console.log(extractRootDomain("websitename.com:1234/dir/file.txt"));
console.log(extractRootDomain("ftps://websitename.com:1234/dir/file.txt"));
console.log(extractRootDomain("example.com?param=value"));
console.log(extractRootDomain("https://facebook.github.io/jest/"));
console.log(extractRootDomain("//***.com/watch?v=ClkQA2Lb_iE"));
console.log(extractRootDomain("http://localhost:4200/watch?v=ClkQA2Lb_iE"));

*点击运行代码段以查看这些方法的实际效果。

*Hit "Run code snippet" to see these methods in action.

无论拥有协议甚至端口号,您都可以提取域名。这是一个非常简化的非正则表达式解决方案,所以我认为这样做。

Regardless having the protocol or even port number, you can extract the domain. This is a very simplified, non-regex solution, so I think this will do.

*谢谢@Timmerz,@ renoirb,@ irinez,@ BigDong,@ ra00l ,@ILikeBeansTacos,@ CharlesRobertson为您的建议! @ ross-allen,感谢您报告错误!

*Thank you @Timmerz, @renoirb, @rineez, @BigDong, @ra00l, @ILikeBeansTacos, @CharlesRobertson for your suggestions! @ross-allen, thank you for reporting the bug!