Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Edit css file using Powershell

theme.css

.actionBlock[selected] {
        color: var(--gray11);
        background-color: var(--gray11);
    }

I want change "color: #FFF"

$file = "theme.css"
$content = Get-Content $file -Raw
$content = $content -replace '(\.actionBlock\[selected\]\s*{[^{}]+color:\s*)([^;]+)(;)', ('$1#FFF; /* EDITED : $2 */')
Set-Content -Path $file -Value $content

Result

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.actionBlock[selected] {
        color: var(--gray11);
        background-color: #FFF; /* EDITED : var(--gray11) */
    }

I want change only color not background-color

>Solution :

Your regex is actually almost fine, you just need to make [^{}]+ match lazily by adding ?. See https://regex101.com/r/K6HTYG/1.

$css = @'
.actionBlock[selected] {
  color: var(--gray11);
  background-color: var(--gray11);
}
'@

$css -replace '(\.actionBlock\[selected\]\s*{[^{}]+?color:\s*)([^;]+)(;)', '$1#FFF; /* EDITED : $2 */'

However this would fail fairly easily as soon as color were after background-color, so perhaps a better approach is to look for the line where color is preceded only by white space. See https://regex101.com/r/99QdN3/1.

$css = @'
.actionBlock[selected] {
  background-color: var(--gray11);
  color: var(--gray11);
}
'@

$css -replace '(?m)(?<=\.actionBlock\[selected\]\s*{[^{}]+?^\s*color:\s*)([^;]+);', ' #FFF; /* EDITED :$1 */'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading