SomeoneS
SomeoneS

Reputation: 1279

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}

Upvotes: 3

Views: 231

Answers (2)

Christoph
Christoph

Reputation: 51181

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_

Upvotes: 5

simoncereska
simoncereska

Reputation: 3043

try [id^=div_] this should help

Upvotes: 4

Related Questions