Home  >  Article  >  Web Front-end  >  JavaScript function code for CSS code compression and formatting_javascript skills

JavaScript function code for CSS code compression and formatting_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:39:25908browse
复制代码 代码如下:

var lCSSCoder={
format : function(s){//格式化代码
s=s.replace(/\s*([\{\}\:\;\,])\s*/g,"$1");
s=s.replace(/\,[\s\.\#\d]*{/g,"{");
s=s.replace(/([^\s])\{([^\s])/g,"$1 {\n\t$2");
s=s.replace(/([^\s])\}([^\n]*)/g,"$1\n}\n$2");
s=s.replace(/([^\s]);([^\s\}])/g,"$1;\n\t$2");
s=s.replace(/;\s*;/g,";");//清除连续分号
return s;
},
pack :function(s){//压缩代码
s=s.replace(/\/\*(.|\n)*?\*\//g,"");//删除注释
s=s.replace(/\s*([\{\}\:\;\,])\s*/g,"$1");
s=s.replace(/\,[\s\.\#\d]*\{/g,"{");//容错处理
s=s.replace(/;\s*;/g,";");//清除连续分号
return s;
}
};

演示代码:
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn