Javascript 从主机名中提取子域
我想要一种从Javascript 从主机名中提取子域,javascript,Javascript,我想要一种从location.hostname提取子域的机制,它应该满足以下所有场景 1. example.com => return value is blank since no sub domain 2. www.example.com => return value is blank since no sub domain 3. test.example.com => return value should be test since this is the su
location.hostname
提取子域的机制,它应该满足以下所有场景
1. example.com => return value is blank since no sub domain
2. www.example.com => return value is blank since no sub domain
3. test.example.com => return value should be test since this is the sub domain
4. example.co.in => return value is blank since no sub domain
5. www.example.co.in => return value is blank since no sub domain
6. test.example.co.in => return value should be test since this is the sub domain
7. 183.87.46.82 => return value is blank since IP passed
仅对于上述给定场景,我需要处理。我不期望有比这更多的东西。最重要的是,我不需要提取任何嵌套的子域名,只有一级子域名就足够了
这方面的任何想法都会有帮助。试试以下方法:
["example.com",
"www.example.com",
"test.example.com",
"http://example.co.in",
"http://www.example.co.in",
"test.example.co.in",
"http://183.87.46.82"]
.filter(function(url){
return url.match(/^(?!www).*\.(.*)\.co.*$/g)
})
更新regex
^(?!www).*\.(.*)\.co.*$
我个人认为代码> www <代码>是一个子域,而在“第二级”域(<代码> .CO.U/<代码>)的情况下,我实际上会考虑<代码> CO/<代码>域名和之前出现的任何一个子域。p> 因为这并不能真正回答您的问题,所以这里有一种方法完全基于您的输入(一旦您发现(该列表并没有涵盖所有内容)比您想象的要难发现,您将对其进行修改)
这样做的目的是应用一个非常直截了当的规则,即“二级”域总是由2x2个字符组成(co.uk
,co.in
,等等),并对这些字符进行过滤,然后跳到现在认为的主域名并跳过它。如果我们最终确定的索引中有某个内容与“www”不匹配,则您可以将其取回
这只是一个例子,向你展示你的问题有多困难,因为它需要一个最新的(如积极维护、策划的)二级域名列表,否则你可能会失败
实际上我唯一考虑的是some.deep.nested.sub.domain.com
将为您提供sub
,而不是some
(还要注意,我根本没有主动阻止ip匹配,只是碰巧匹配了2x2规则)
我很好奇你试图通过隔离子域来解决的问题,因为它本身没有任何意义。我可以想象这样的情况:您希望显示基于子域的某种“昵称”,但我相信您会知道预期的模式。从技术角度来看,只有子域是没有用的。请考虑以下定义有效主机名的文章:
此正则表达式应该对您的情况有所帮助:
var regex = /^(?!www\.|\d{1,3}\.)[a-z0-9-]+?\.[a-z0-9-]{3,}\.[a-z0-9-]+?(\.[a-z0-9-]+?)*?$/gi;
var hostname = "example.com";
console.log(hostname.match(regex)); // null
hostname = "www.example.com";
console.log(hostname.match(regex)); // null
hostname = "test.example.com";
console.log(hostname.match(regex)); // [ "test.example.com" ]
hostname = "www.example.com";
console.log(hostname.match(regex)); // null
hostname = "example.co.in";
console.log(hostname.match(regex)); // null
hostname = "www.example.co.in";
console.log(hostname.match(regex)); // null
hostname = "1test.example.co.in";
console.log(hostname.match(regex)); // [ "1test.example.co.in" ]
hostname = "187.162.10.12";
console.log(hostname.match(regex)); // null
以下答案可能会帮助您投票关闭,因为SO不是一项代码编写服务。然后,您需要了解所有SLD(子级别域),如co.uk,并能够识别什么是ip地址,什么不是ip地址。我所知道的唯一一个库就是这个地址如何
wiki.wargaming.co.uk
?更新正则表达式以支持wiki.wargaming.co.uk
URL谢谢……这就是我要找的for@user850234请考虑我在回答中提出的所有问题,它们现在可能不适用。但最终,技术有一个让人措手不及的坏习惯。是的……我考虑了所有这些……我试图解决的问题是,我的应用程序中有多个子域名登录,用户界面必须从URL中提取,然后使用相同的子域名与API进行后续通信,以防万一无效子域的显示用户友好消息并加载相关屏幕
var regex = /^(?!www\.|\d{1,3}\.)[a-z0-9-]+?\.[a-z0-9-]{3,}\.[a-z0-9-]+?(\.[a-z0-9-]+?)*?$/gi;
var hostname = "example.com";
console.log(hostname.match(regex)); // null
hostname = "www.example.com";
console.log(hostname.match(regex)); // null
hostname = "test.example.com";
console.log(hostname.match(regex)); // [ "test.example.com" ]
hostname = "www.example.com";
console.log(hostname.match(regex)); // null
hostname = "example.co.in";
console.log(hostname.match(regex)); // null
hostname = "www.example.co.in";
console.log(hostname.match(regex)); // null
hostname = "1test.example.co.in";
console.log(hostname.match(regex)); // [ "1test.example.co.in" ]
hostname = "187.162.10.12";
console.log(hostname.match(regex)); // null