如何在属性选择器中使用/模拟正则表达式的反向引用? [英] How can I use/emulate regex-like backreferences in attribute selectors?

查看:141
本文介绍了如何在属性选择器中使用/模拟正则表达式的反向引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是写一个选择器,在一个地方匹配任意值,然后需要不同的值等于它。如果 [attr =value] 作为正则表达式解析value,那么这将解决我的问题:

  * [class =(。+)] * [class =if_\1] {/ * styles * /} 



显然,我可以单独列出每个可能的类,但这需要很大的方便。



这可能吗?

解决方案

不,这是不可能的。属性选择器几乎是完全静态的,并且几乎不提供动态匹配功能(除了子串匹配之外,它们仍然是静态的,而不是基于动态模式的)。



样式表预处理器(如Sass或LESS)将允许您生成所需的静态CSS规则,但所有这是自动化手动任务列出所有可能的值单独,这证明了我的第一点。


What I want to do is write a selector that matches an arbitrary value in one place, then later requires a different value be equal to it. If [attr="value"] parsed "value" as a regex, then this would solve my problem:

*[class="(.+)"] *[class="if_\1"] {/* styles */}

Obviously I could just list each possible class individually, but that takes a great deal of convenience out of it.

Is this possible?

解决方案

No, it's not possible. Attribute selectors are almost completely static and provide almost no dynamic matching functionality (beyond that of substring matches, which are still static and not dynamic pattern-based). They do not support anything like what you see in everyday regular expressions.

A stylesheet preprocessor such as Sass or LESS will allow you to generate the static CSS rules needed, but all that does is automate the manual task of listing all possible values individually, which proves my first point.

这篇关于如何在属性选择器中使用/模拟正则表达式的反向引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆