首页 > 其他分享 >从url地址获取主机名

从url地址获取主机名

时间:2022-08-24 20:35:06浏览次数:67  
标签:url host 主机名 地址 var null match

        function getHost(url) {
            var host = "null";
            if(typeof url == "undefined"|| null == url) {
            url = window.location.href;
        }
        var regex = /^\w+\:\/\/([^\/]*).*/;
        var match = url.match(regex);
        if(typeof match != "undefined" && null != match) {
            host = match[1];
        }
        return host;
    }
    console.log(getHost('https://i.cnblogs.com/articles'))

标签:url,host,主机名,地址,var,null,match
From: https://www.cnblogs.com/zhulongxu/p/16621457.html

相关文章