1

I want to try filter all selector using js reg. Predict I want to match div#abc.read-more.(Please note that div, #abc, .read-more all can exist or not.But if div exist, it should be at the beginning). I list all situations I know in two array: matched and unmatched. Here's the test code . I use jasmine test engineer.

The reg pattern I use now is /((\bdiv)+|(\.read-more)+|(#abc)+)(\s*|(:\w+)+)\s*(((?=,)(.*))|$)/

But there's still have two situations not being pass. .read-morediv#abc and #abcdiv.read-more. Common thing is div is at the middle.

Sorry, But I just get that there's two more situations will fail too: div.read-morediv#abc and div#abcdiv.read-more. These two both should be unmatched.

Anyone can help here? And I will very appreciate If you can explain how you get your solution. :D

asked Dec 17, 2013 at 2:30
4
  • Why are you doing this if I may ask? You should be able to use the DOM. Commented Dec 17, 2013 at 2:34
  • #abcdiv.read-more would match the element with ID abcdiv and class read-more. Is that really what you want? Type selectors must always be at the beginning: w3.org/TR/CSS2/selector.html#selector-syntax. Commented Dec 17, 2013 at 2:41
  • @elclanrs, what I plan to do is emulate devtool or firebug. user click one elemnt , then show all relative style to user. Commented Dec 17, 2013 at 4:04
  • @FelixKling, yes. I try to match selector. And some class like .divider will match div, right? I need to filter these out. Commented Dec 17, 2013 at 4:06

1 Answer 1

1

For div.read-morediv#abc:

((\bdiv)+|(.read-more)+|(#abc)+) matches the final '#abc'

(\s*|(:\w+)+) matches the following 0 spaces (\s*)

\s* again matches 0 spaces

(((?=,)(.*))|$) matches the end of the string (via the |$)

The pattern for the other example is similar. In both cases, the presence of 'div' is entirely ignored in generating the match, because another part of the 3-part OR in the first group is sufficient.

answered Dec 17, 2013 at 3:27

2 Comments

yes, those case behavior as you said. Do you have any solution? So far, I still can figure out one solution for these situations. :)
Check out this question - I think it'll help. The negative lookbehind is really what you wanted here (to verify that div doesn't occur after the other two), but there are some workarounds. stackoverflow.com/questions/13458946/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.