简体   繁体   中英

javascript reg pattern match css selector

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM