Using regular expression in css?

You can manage selecting those elements without any form of regex as the previous answers show, but to answer the question directly, yes you can use a form of regex in selectors:

#sections div[id^='s'] {
  color: red;
}
<div id="sections">
  <div id="s1">one</div>
  <div id="s2">two</div>
  <div id="s3">three</div>
  <div id="t1">four</div>
</div>

That says select any div elements inside the #sections div that have an ID starting with the letter ‘s’.

See fiddle here.

W3 CSS selector docs here.

Leave a Comment