href match with links
I've got this pi开发者_如何学Cece 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?
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
/* 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
/* 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.
if ($link.match(/^(?:.*?\.)?(domain)\.net(?:\/.*)?$/).length == 0) {
$(this).attr("href",'http://links.domain.net/?url='+$link);
}
精彩评论