How can I use / emulate regex-like backreferences in attribute selectors?

What I want to do is write a selector that matches an arbitrary value in one place and then requires another value to be equal to it. If [attr="value"]

parsed "value" as a regex, this would solve my problem:

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

      

Obviously, I could just list each possible class separately, but that takes a lot of convenience.

Is it possible?

+1


source to share


1 answer


No, It is Immpossible. Attribute selectors are almost entirely static and have little or no dynamic matching functionality (other than substring matches, which are still static rather than dynamic based on templates). They don't support anything like what you see in everyday regular expressions.



A style sheet preprocessor like Sass or LESS will allow you to generate static CSS rules, but anything that does it automates the manual task of listing all possible values โ€‹โ€‹individually, which proves my first point.

+1


source







All Articles