在最好的情况下,我如何检查一个链接属于*.domain.com

In best case how can I check a link belongs to *.domain.com?

本文关键字:一个 链接 com domain 属于 检查 情况下 何检查      更新时间:2023-09-26

如何使用javascript检查链接属于域名*.domain.com ?

我的意思是忽略子域

如果我们讨论的是页面上的链接(<a>元素),那么试试这个:

// regular expression to check for domain.com
// won't match tricky ones like domain.company.com nodomain.com :)
var re = /(^|'.)domain'.com('/|$)/i; 
// loop through all the links
$("a").each(function() {
  // test if href attribute belongs to domain.com
  if(re.test($(this).attr('href'))) {
    // do what you want with links that belong to domain.com
    $(this).addClass("domaincom");
  }
});

这里是代码