1

I just had a case where it would have been really useful if there were a CSS selector like :numeral. I could then make all the numerals superscript and change their color.

It then occurred to me that it would be even more useful to have something like :regex('...') which would allow me to use regex to select particular text (numerals preceded by a space, for example: :regex(' \\d*')).

Is there a good reason this sort of selection should not be implemented in CSS Spec?

1
  • @Steeven: Re your suggested edit, "if there were" is the correct construction (it's the "subjunctive" or "speculative" tense). Commented Jul 5, 2011 at 15:36

1 Answer 1

3

Is there a good reason this sort of selection should not be implemented in CSS Spec?

Yes - performance reasons.

A common request is a "parent selector" (similar to jQuery's :has() selector), which is also rejected for similar performance reasons.

A good read on this subject is: http://snook.ca/archives/html_and_css/css-parent-selectors

The sheer length of that article is why I'm not attempting to explain this myself.

If a parent selector is too bad from a performance viewpoint, you can imagine that a "regex selector" would be unthinkable.

Especially when you consider that not all regexes are equal: it's very easy to make very slow regexes.

For example: http://www.regular-expressions.info/catastrophic.html

Sign up to request clarification or add additional context in comments.

8 Comments

Trying to find sources citing the reason why the :contains() pseudo-class was dropped, but to no avail.
Parent selectors and a :regex selector are fundamentally different, I wouldn't generalize from performance aspects of the one to comment on performance aspects of the other.
This is true, partially. I disagree in that the scope of supported regex could be reduced to avoid "catastrophes" (although having said that, I question it to some degree). Nevertheless, the problem with parent (ancestor) selectors - with which I am familiar - is that later dom affects earlier rendering. This is not the case with regex because it has to match an expression.
@T.J. Crowder: I agree that my reasoning is faulty. I just went with what I could think of :( I am at least correct with the "performance" idea in my answer, see the red text in an older revision of the CSS3 spec. If :contains has "performance issues", then :regex would be much, much worse.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.