Regex 正则表达式删除空白

Regex 正则表达式删除空白,regex,Regex,我需要一个正则表达式来删除css字符串中的所有空格,除了选择器之间的空格 例如: 输入 输出 #id .class .anotherclass{color:#555}#anotherid .class{color:#fff;} 我假设您正在使用php,基于此,请尝试以下方法: <? $text = "#id .class .anotherclass { color: #555 } #anotherid .class { color : #fff; }"; function removes

我需要一个正则表达式来删除css字符串中的所有空格,除了选择器之间的空格 例如: 输入

输出

#id .class .anotherclass{color:#555}#anotherid .class{color:#fff;}

我假设您正在使用php,基于此,请尝试以下方法:

<?
$text = "#id .class .anotherclass { color: #555 } #anotherid .class { color : #fff; }";
function removespaces($matches)
{
return $matches[1].str_replace(" ", "", $matches[2]);
}
echo preg_replace_callback( "/(#.*?)(\{.*?\})/i", "removespaces", $text);
?>

演示:

<?
$text = "#id .class .anotherclass { color: #555 } #anotherid .class { color : #fff; }";
function removespaces($matches)
{
return $matches[1].str_replace(" ", "", $matches[2]);
}
echo preg_replace_callback( "/(#.*?)(\{.*?\})/i", "removespaces", $text);
?>