user194076
user194076

Reputation: 9017

Regex does not work as excpected

see on jsfiddle. http://jsfiddle.net/yNJKj/13/

This regex is supposed to show only top level items. but it somehow sohws 2nd level items in SOME categories.

WHat am I doing wrong? Thanks!

Upvotes: 0

Views: 64

Answers (3)

sdleihssirhc
sdleihssirhc

Reputation: 42496

My guess is it's this part, at the end of the regex:

\/.*[a-zA-Z]$

You're telling it to match the following:

  1. A slash /
  2. Anything of any length
  3. An upper or lowercase letter
  4. The end of the string

I'm not 100% on what you are and aren't trying to match, but that wildcard part (.*) is probably matching all the things you want, as well as what you don't. Making the regex more specific could help.

On an unrelated note, it would probably be more semantic and easier to work with if, instead of a single unordered list and  , you reworked it to utilize nested lists. Something like this:

<ul>
    <li>
        <a href="*">Category</a>
        <ul>
            <li><a href="*">Sub-Category</a></li>
            <li><a href="*">Sub-Category</a></li>
        </ul>
    </li>
</ul>

However, if you do decide to stick with the non-breaking spaces, then couldn't you use those to decide what gets hidden? It looks like all you would have to do is hide anything that starts with &nbsp;, and that would accomplish what you want.

Upvotes: 1

Distdev
Distdev

Reputation: 2312

I think you should change regexp to /^.*\/activity\/[a-zA-Z]*$/i, because . matchs all characters including /

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074198

I think the regex you want is:

/^.*\/activity\/[^/]*$/i

Updated fiddle

Or possibly

/^.*\/activity\/[^/]*[a-zA-Z]$/i

Updated fiddle

(I'm not sure why you have that single-character [a-zA-Z] match at the end.)

Your original

/^.*\/activity\/.*[a-zA-Z]$/i

...allows / to be matched by the .* preceding your [a-zA-Z], hence including sub-categories.

Upvotes: 5

Related Questions