I'm trying to code a regular expression that will subtract a class name out of a CSS rule (through a capturing group), if the CSS rule contains certain property.
For instance:
var test = `
.myUglyClass {
propOne: abc;
notSpecialProp: def;
propThree: ghi;
}
.myClass {
propOne: abc;
specialProp: def;
propThree: ghi;
}
`;
If .myClass
has property specialProp
(in this example, that is the case), then subtract .myClass
from the CSS rule structure, but only the class name of the class that contains the property.
I have tried this, which uses a positive lookahead:
test.match(/(\.\w*)(?=\s*{[^]+specialProp:[^]+})/);
It kind of works, but if the property specialProp
is contained within any class, it will match all class names.
How can I make it so that it only matches the class name that contains the special property, when there are other class names on the string? I guess I could think of another way of achieving the same thing, but I feel I'm kind of close to the regex solution.
Thank you.