我試圖改變無效的CSS是這樣的:正則表達式來添加缺少的選擇無效CSS
{color:red}
,並使其有效像這樣:
.missing_selector{color:red}
範例CSS:
a {
color:black;
background-color:blue;
}
{color:red}
<!-- TEST -->
@media screen and (max-width:620px) {
/* TEST 2 */
a.test {color:blue;}
p[class="test2"] {color:green;}
}
我目前正則表達式:
/([^a-z0-9\)\];\-_]*\{)/i
現場測試:
http://www.phpliveregex.com/p/eMq
只是一個提示,而不是答案,但也許這個庫可以幫助:https://github.com/sabberworm/PHP-CSS-Parser我想,僅僅使用正則表達式的解決方案相當容易出錯。 – Jan
您可以嘗試['/^\h*\K(?={)/m'](https://regex101.com/r/mC7pI5/1)並替換爲'.missing_selector' –