1

I'm struggling to match and keep all CSS rules containing the string 224,13,78.

Sample input:

h1 {
    color: rgb(224,13,78);
}


h1 {
    display: block; /* Delete this whole block */
}


#test-color {
    color: rgb(224,13,78);
}    


.test-position {
    position: absolute; /* Delete this whole block */
}


.test-bg-color {
    background-color: rgba(224,13,78,0.5);
}


@media ( max-width: 1200px ) {
    .test-color {
        color: rgb(224,13,78);
    }

    .test-position {
        overflow: hidden; /* Delete this whole block */
    }
}

Desired output:

h1 {
    color: rgb(224,13,78);
}


#test-color {
    color: rgb(224,13,78);
}


.test-bg-color {
    background-color: rgba(224,13,78,0.5);
}


@media ( max-width: 1200px ) {
    .test-color {
        color: rgb(224,13,78);
    }
}

Is there a handy regex that solves my problem?

For reference, I found this solution, but it matches the property name, not the value:

Find: \{.*(font-size\:\s*\d+px\;).*\}?

Replace: \{ $1 \}

And also this JavaScript solution that does the same:

What is RegEx to remove css keeping font size

I accept a JavaScript solution too, but PHP is preferable.

Thank you all in advance.

1 Answer 1

1

Try the following regex:

\h*+[^\s{}][^{}]*{(?:[^{}](224,13,78)?)++[^{}]*+}(?(1)(*SKIP)(*F))\R*

See live demo here

What it does is trying to match a sequence of characters 224,13,78 in a {...} block. If it matches it holds the value in the first capturing group. Then later we say if 1st capturing group was successful in matching, then skip over whatever has been matched by:

(?(1)(*SKIP)(*F))

otherwise the whole match doesn't contain the desired characters and engine won't skip over it. We now only need to replace matched blocks with nothing:

$css = preg_replace('~\h*+[^\s{}][^{}]*{(?:[^{}](224,13,78)?)++[^{}]*+}(?(1)(*SKIP)(*F))\R*~', '', $css);
Sign up to request clarification or add additional context in comments.

8 Comments

Excelent @revo, it works, thank you! Do you have a idea to clean any other property inside every matched block and keep only the line containing 224,13,78? Input: h1 { position: absolute; color: rgb(224,13,78); display: block; } Output: h1 {color: rgb(224,13,78);}
My pleasure. Just run another preg_replace on the previous output with this \s*[^\s:{}]+:((?!224,13,78)[^;])*+;. See live demo here regex101.com/r/Q9W14l/3
Great, you helped me a lot. Thank you again!
Yes, but it will remove leading newline characters as well which makes CSS less readable. Try \h*[^\s{}][^{}]*{(?:[^}](224,13,78)?)+[^}]*}(?(1)(*SKIP)(*F))\R* instead.
Replace [^;]* with [^;{}]*.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.