【发布时间】:2019-04-05 13:35:53
【问题描述】:
我正在努力匹配并保留所有包含字符串 224,13,78 的 CSS 规则。
示例输入:
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 */
}
}
期望的输出:
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);
}
}
是否有一个方便的正则表达式可以解决我的问题?
作为参考,我找到了这个解决方案,但它匹配属性名称,而不是值:
查找:\{.*(font-size\:\s*\d+px\;).*\}?
替换:\{ $1 \}
还有这个 JavaScript 解决方案也是如此:
What is RegEx to remove css keeping font size
我也接受 JavaScript 解决方案,但最好使用 PHP。
提前谢谢大家。
【问题讨论】:
标签: php regex replace preg-replace preg-match