如何使用正则表达式过滤元素

时间:2015-02-15 15:20:31

标签: javascript jquery regex

我想选择所有指向外部链接的元素,所以我想要一个只选择与

匹配的href值的选择器
/https?:\/\/[^\/\!\?\<\>\_\*\&\(\)\#\$\@]*/

不同

window.location.hostname

什么是正确的表达?

3 个答案:

答案 0 :(得分:0)

$("a:not([href*="+window.location.hostname+"])[href*=http], a:not([href*="+window.location.hostname+"])[href*=https]")

将为您提供所有不具有window.location.hostnamehttphttps

的链接

答案 1 :(得分:0)

这将是一个更强大的解决方案,检查不同的位置主机名而不是主窗口:

var $externalLinks = $('a[href]').filter(function(){
    return this.hostname != window.location.hostname;
});

-jsFiddle-

答案 2 :(得分:-2)

不需要正则表达式。 试试这个(使用Jquery):

var links = $('a[href^="http"]');
相关问题