gpt4 book ai didi

javascript - 正则表达式使用 jQuery 在新窗口中打开主机名中不包含 "xyz"的链接?

转载 作者:行者123 更新时间:2023-11-30 18:14:39 25 4
gpt4 key购买 nike

我需要添加一个 jQuery 检查来启动我网站上指向外部网站的任何链接。我怎样才能做到以下几点:

查找主机名中不包含“xyz”的任何 anchor 链接。例如:

  • http://xyz.com
  • http://asfasdfadsfadsf.xyz.com

它必须忽略任何在主机名后包含 xyz 的链接:

  • http://someothersite.com/xyz
$('a[href^="http://%xyz%"]').filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).attr('target', '_blank');

最佳答案

!(/https?:\/\/[^\/]*xyz.*/i.test(link))

参见 this demo .


JSHint示例:

/*global $:false */

(function() {

"use strict";

$('a').filter(function() {
return !(/https?:\/\/[^\/]*xyz.*/i.test($(this).attr('href')));
}).text("***");

}());

关于javascript - 正则表达式使用 jQuery 在新窗口中打开主机名中不包含 "xyz"的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com