简单实现一个css代码格式化功能
如果 你想自己实现一个格式化css代码的小工具的话可以参考一下代码,使用方法:
var str=`.evaluateContainer{padding:10px 20px;margin:20px}`
CSSencode(str)
function CSSencode(code)
{
code = code.replace(/\r\n/ig,'');
code = code.replace(/(\s){2,}/ig,'$1');
code = code.replace(/\t/ig,'');
code = code.replace(/\n\}/ig,'\}');
code = code.replace(/\n\{\s*/ig,'\{');
code = code.replace(/(\S)\s*\}/ig,'$1\}');
code = code.replace(/(\S)\s*\{/ig,'$1\{');
code = code.replace(/\{\s*(\S)/ig,'\{$1');
return code;
}
function CSSdecode(code)
{
code = code.replace(/(\s){2,}/ig,'$1');
code = code.replace(/(\S)\s*\{/ig,'$1 {');
code = code.replace(/\*\/(.[^\}\{]*)}/ig,'\*\/\n$1}');
code = code.replace(/\/\*/ig,'\n\/\*');
code = code.replace(/;\s*(\S)/ig,';\n\t$1');
code = code.replace(/\}\s*(\S)/ig,'\}\n$1');
code = code.replace(/\n\s*\}/ig,'\n\}');
code = code.replace(/\{\s*(\S)/ig,'\{\n\t$1');
code = code.replace(/(\S)\s*\*\//ig,'$1\*\/');
code = code.replace(/\*\/\s*([^\}\{]\S)/ig,'\*\/\n\t$1');
code = code.replace(/(\S)\}/ig,'$1\n\}');
code = code.replace(/(\n){2,}/ig,'\n');
code = code.replace(/:/ig,':');
code = code.replace(/ /ig,' ');
return code;
}
发表评论 (审核通过后显示评论):