我有这个正则表达式:
/(((\w+)|(\.\w+)|(\#\w+)|\*)(\[(.+(=".+"|\*".+"|\^".+"|))\])?(::|:)?)+(?=[ \S]*\{)/gm
我试图使用它来匹配CSS选择器。考虑这个伪代码 CSS 输入:
.main {
property: value;
}
.one, .two a[href$=".com"] {
.subclass {
property: value;
}
}
.test:before, .test:after:active {}
上述模式将返回以下匹配:
['.body', '.one', '.two', 'a[href$=".com"]', '.subclass', '.test:before', '.test:after:active']
我正在尝试修改模式以便不匹配伪选择器。因此,所有其他匹配仍应有效,但
.test:before
应仅匹配.test
,.test:after:active
也应仅匹配.test
。我无法想到一种方法来实现这一点,而不使用负回溯或者不匹配第一个字符是:
的方式。我是在Node中实现这个操作的,我不想将我的脚本锁定在Node > 9.2.0上,只是为了在正则表达式中使用负回溯。
非常感谢您提供任何想法!