简体   繁体   English

href与链接匹配

[英]href match with links

I've got this piece of code, but i wanna fix it because the if statement if it's true it's empty... any ideas of how i can make it false always? 我有这段代码,但是我想修复它,因为如果if语句为true,那么它为空...关于如何使它始终为false的任何想法?

using ! 使用! inside the if it's not working 在里面,如果它不工作

var $link = $(this).attr("href");
if ($link.match(/^(?:.*?\.)?(domain)\.net(?:\/.*)?$/))  {
}else {
    $(this).attr("href",'http://links.domain.net/?url='+$link);
}

This it's in jquery in this function 这是在此功能的jQuery中

/* change_links */
(function($) {
    $.fn.change_links = function() {
        $('.post a:not([href^=mailto])').each(function() {
            var $link = $(this).attr("href");
            if ($link.match(/^(?:.*?\.)?(taringacs)\.net(?:\/.*)?$/))  {
            }else {
                $(this).attr("href",'http://links.taringacs.net/?url='+$link);
            }
        });
    }
})(jQuery);

thanks in advance 提前致谢

Use RegExp.test 使用RegExp.test

/* change_links */
(function($) {
    $.fn.change_links = function() {

        $('.post a:not([href^=mailto])').each(function() {
            var $link = $(this).attr("href");

            if (! /^(?:.*?\.)?(taringacs)\.net(?:\/.*)?$/.test($link))  {
                $(this).attr("href",'http://links.taringacs.net/?url='+$link);
            }

        });

    }

})(jQuery);

The match() method returns an array of matches, you'll have to check its length. match()方法返回一个匹配数组,您必须检查其长度。

if ($link.match(/^(?:.*?\.)?(domain)\.net(?:\/.*)?$/).length == 0)  {
    $(this).attr("href",'http://links.domain.net/?url='+$link);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM