Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS and regex (?)

I was wondering is there way to do something like regex replace in css file?

For example: i have lot of selectors like these:

 #div_23
 #div_45
 #div_8

Let's say that i cant use classes to add styling, is there way to do something like this in CSS file:

 #div_[0-9] {background:#000000}
like image 234
SomeoneS Avatar asked Oct 20 '25 05:10

SomeoneS


1 Answers

You can partly match attributes (css2.1 doc / css3 doc) - note however that these rules are rather slow. Especially in your case, where you have to use ultra-slow attribute selectors to match ids which would be blazing fast when using #.

I prefer using hyphenated Ids and classes, I find them more readable and this way you can use the following matcher:

[att|=val]

Applied to your case:

#div-23
#div-45
#div-8
#div

would all match

[id|=div]{background:#000000}

However, CSS3 provides the following attribute selector (which is supported in all major browsers) which would suit your case perfectly:

[att^=val]

so you could write:

[id^=div_]{background:#000000}

to match

#div_23
#div_45
#div_8
/* and */
#div_
like image 57
Christoph Avatar answered Oct 21 '25 18:10

Christoph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!