有很多帖子都在寻找解析网址并获取主机名的方法。通常的解决方案是创建文档元素,设置URL并访问.hostname属性。这是一个很好的解决方案。我很难超越这种技术。
我有一个成功从主机名中提取基本主机的功能。为了描述基本主机的意思(不确定正确的命名法),我将展示该功能并给出一些示例输入输出。
function parseURL(url) {
var parser = document.createElement('a');
parser.href = url;
url = parser.hostname;
//get a version of the url with the last "." and everything beyond it truncated.
//Uses this as a trick in the next step to get the "second to last" index.
url = url.substr(0, url.lastIndexOf("."));
//get a version of the url with everything before the second to last "." truncated.
url = parser.hostname.substr(url.lastIndexOf(".")+1);
return url;
};
parseURL("http://code.google.com/p/jsuri/")
//google.com - I don't think jsuri handle hosts any more effectively
parseURL("http://www.nytimes.com/pages/nyregion/index.html")
//nytimes.com
parseURL("http://fivethirtyeight.blogs.nytimes.com/2013/01/12/in-cooperstown-a-crowded-waiting-room/"
//nytimes.com
parseURL("http://www.guardian.co.uk/uk/2013/jan/13/fears-lulworth-cove-development-heritage"
//co.uk
最后一个例子是我担心的例外,以及为什么我在寻找更可行的解决方案。获取主机的.hostname方法是一个很好的第一步,我正在寻找一种更好的方法来攻击有时在基本主机之前的子主机。
任何帮助表示赞赏(如果只是纠正我的术语)。
答案 0 :(得分:0)
您应该能够根据ccTLD(例如示例中的.uk
)总是2个字符(为了清晰起见而声明的变量)这一事实来分支您的代码:
// Grab the last bit (the top level domain)
var tld = url.subtr(url.lastIndexOf("."))
if (tld.length === 2)
//do stuff
else if (tld.length === 3)
//do other stuff
此外,我相信您正在寻找的字词是“域名”,不过有些内容确实包含“子域名”(docs.google.com中google之前的位)。
答案 1 :(得分:0)
当我想解析一个URL时,我会做这样的事情
function parseURL(url) {
var a = document.createElement('a'), obj, i, j;
a.href = url;
obj = {
'domain': '',
'hash': a.hash.slice(1),
'host': a.host,
'hostname': a.hostname,
'href': a.href, // copy back from <a>
'origin': a.origin,
'pathname': a.pathname,
'port': a.port,
'protocol': a.protocol.slice(0, -1),
'search': a.search.slice(1),
'subdomain': ''
};
i = obj.hostname.lastIndexOf('.');
if (obj.hostname.length - i === 3) { // if .yz
j = obj.hostname.lastIndexOf('.', i-1);
if (j === i - 3 || j === i - 4) { // test .vwx.yz or .wx.yz
i = j;
}
}
j = obj.hostname.lastIndexOf('.', i-1);
if (j !== -1) { // move back one more .
i = j;
}
obj.domain = obj.hostname.slice(i+1);
obj.subdomain = obj.hostname.slice(0, i);
return obj;
};
现在如果你使用它,
var myURL = parseURL('http://www.example.co.uk:8080/hello/world.html?foo=bar#anchor');
/* {
"domain": "example.co.uk",
"hash": "anchor",
"host": "www.example.co.uk:8080",
"hostname": "www.example.co.uk",
"href": "http://www.example.co.uk:8080/hello/world.html?foo=bar#anchor",
"origin": "http://www.example.co.uk:8080",
"pathname": "/hello/world.html",
"port": "8080",
"protocol": "http",
"search": "foo=bar",
"subdomain": "www"
} */
因此,根据您的需要,您可以使用myURL.domain
(或从函数中移除其余部分)
答案 2 :(得分:0)
function parseURL(str) {
var re = /^(?:([a-zA-Z]+:)\/\/)?(?:([-+._a-zA-Z0-9]+)(?::([-+._a-zA-Z0-9]+))?@)?(([^-~!@#$%^^&*\(\)_+=\[\]{}:;'"\\,.\/?\s]+(?:[^~!@#$%^^&*\(\)_+=\[\]{}:;'"\\,.\/?\s]+[^-~!@#$%^^&*\(\)_+=\[\]{}:;'"\\,.\/?\s])*(?:\.[^-~!@#$%^^&*\(\)_+=\[\]{}:;'"\\,.\/?\s]+(?:[^~!@#$%^^&*\(\)_+=\[\]{}:;'"\\,.\/?\s]+[^-~!@#$%^^&*\(\)_+=\[\]{}:;'"\\,.\/?\s])*)*)(?::(\d+))?)?(\/[^?#]*)?(\?[^#]*)?(#.*)?$/;
var scheme = ['protocol', 'user', 'password', 'hostname', 'host', 'port', 'pathname', 'search', 'hash'], parts = re.exec(str);
if (parts != null) {
for (var i = 0, l = scheme.length, obj = {}; i < l;) {
obj[ scheme[i] ] = parts[++i] != undefined ? parts[i] : '';
}
return obj;
}
return false;
}
答案 3 :(得分:0)
我经常使用此函数从URL解析主机:
function urlParseHost(url){
var re = new RegExp("^(?:f|ht)tp(?:s)?\://([^/]+)", "im");
return(url.match(re)[1].toString());
}
您可以从GitHub here获取工作代码。