There is no way to use regular expressions with CSS. However, there are ways to select elements which attributes start/end/contain certain strings.
In this case, I believe you're trying to select all elements for which their class
doesn't start with col-
. You can use this:
*:not([class ^= "col-"]) {
max-width: 100%;
}
Note that [class ^= "col-"]
will only match if the class
attribute starts with col-
, so <element class="col-123 abc">
will match but <element class="abc col-123">
will not.
Because of that, it's recommended to have a separate class which is the same for all elements. You can do it with JavaScript:
// Note: This code isn't very efficient. Add the class manually whenever possible.
const tw = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT, {
acceptNode(elem) {
if ([...elem.classList].some(c => c.startsWith("col-")))
return NodeFilter.FILTER_ACCEPT;
else
return NodeFilter.FILTER_SKIP;
}
});
while (tw.nextNode()) {
tw.currentNode.classList.add("col");
}
.col {
color: red;
}
<div class="abc col-12">Test div</div>
<div class="abc">Another test div</div>