【发布时间】:2015-04-16 03:32:58
【问题描述】:
我想选择所有指向外部链接的 a 元素,所以我想要一个只选择匹配的 href 值的选择器
/https?:\/\/[^\/\!\?\<\>\_\*\&\(\)\#\$\@]*/
不同于
window.location.hostname
正确的表达方式是什么?
【问题讨论】:
标签: javascript jquery regex
我想选择所有指向外部链接的 a 元素,所以我想要一个只选择匹配的 href 值的选择器
/https?:\/\/[^\/\!\?\<\>\_\*\&\(\)\#\$\@]*/
不同于
window.location.hostname
正确的表达方式是什么?
【问题讨论】:
标签: javascript jquery regex
这将是更强大的解决方案,检查与主窗口不同的位置主机名:
var $externalLinks = $('a[href]').filter(function(){
return this.hostname != window.location.hostname;
});
【讨论】:
$("a:not([href*="+window.location.hostname+"])[href*=http], a:not([href*="+window.location.hostname+"])[href*=https]")
会给你所有没有window.location.hostname但有http或https的链接
【讨论】:
不需要正则表达式。 试试这个(使用 Jquery):
var links = $('a[href^="http"]');
【讨论】: