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, 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.