0

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 not 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;
});
1
  • 2
    if all you're doing is matching the beginning of the value, regex is overkill. use .indexOf instead. Commented Feb 4, 2011 at 5:52

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This solves problem partially. I cannot find an error. When I click a link, all other links are getting displayed, not only that are matched pattern. Do you see an error? Code is at the top of the page
1

check this

use indexOf(//value to search) for matching

hope it helps

2 Comments

it does not work. Still shows all things. Just does not run correctly. See: jsfiddle.net/yNJKj/4
Thank you so much. It finally works! I really appreciate this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.