简体   繁体   English

javascript reg模式匹配css选择器

[英]javascript reg pattern match css selector

I want to try filter all selector using js reg. 我想尝试使用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). 预测我要匹配div#abc.read-more 。(请注意div#abc.read-more都可以存在或不存在,但是如果div存在,则应该在开头)。 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*(((?=,)(.*))|$)/ 我现在使用的reg模式是/((\\bdiv)+|(\\.read-more)+|(#abc)+)(\\s*|(:\\w+)+)\\s*(((?=,)(.*))|$)/

But there's still have two situations not being pass. 但是仍然有两种情况没有通过。 .read-morediv#abc and #abcdiv.read-more . .read-morediv#abc#abcdiv.read-more Common thing is div is at the middle. 常见的是div在中间。

Sorry, But I just get that there's two more situations will fail too: div.read-morediv#abc and div#abcdiv.read-more . 抱歉,但是我只知道还有另外两种情况也会失败: div.read-morediv#abcdiv#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 :d

For div.read-morediv#abc: 对于div.read-morediv#abc:

((\\bdiv)+|(.read-more)+|(#abc)+) matches the final '#abc' (((bdiv)+ |(.read-more)+ |(#abc)+)匹配最终的'#abc'

(\\s*|(:\\w+)+) matches the following 0 spaces (\\s*) (\\ s * |(:\\ w +)+)匹配以下0个空格(\\ s *)

\\s* again matches 0 spaces \\ s *再次匹配0个空格

(((?=,)(.*))|$) 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. 在这两种情况下,在生成匹配项时都会完全忽略“ div”的存在,因为第一组中三部分OR的另一部分就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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