regex value is not matching jquery
I am trying to match clicked linked path with other links. It does not work at all. Here's the code. I need to link's href starts with the regex.
$('#taxonomylist ul li a').click(function() {
$href = $(this).attr("href");
$regex = new RegExp("^"+$href);
$('#taxonomylist ul li').each(function() {
$href_sub = $("a", this).attr("href");
if ($href_sub.match($regex))
{
$(this).css("display", "block");
}
});
return false;
});
Here's what I got in firebug:
$href = "/?q=category/activity/test"
$href_sub = "/?q=category/activity/test/lamp"
$regex = /^\/?q=category\/activity\/test/
Everything seems find, but it does n开发者_运维问答ot work as expected. If I remove matching by regex, everything works fine (without filtering, of course).
EDIT:
Now it partially works, only assign css property to all links, not only those that match value. Does anyone see the problem?
$('#taxonomylist ul li a').click(function() {
$href = $(this).attr("href");
$regex = new RegExp("^"+$href);
$('#taxonomylist ul li').each(function() {
$href_sub = $("a", this).attr("href");
if ('$href_sub:contains($href)')
{
$(this).css("display", "block");
}
});
return false;
});
Because you're asking if the regex matches your string. Meaning, your regex should at least look like this (based on your example):
$regex = /^\/?q=category\/activity\/test\/\w+/
You might want to use :contains()
selector instead.
check this
use indexOf(//value to search)
for matching
hope it helps
精彩评论