admin管理员组文章数量:1290317
I have a strait forward aggregator/minimizer/cacher I've written in node.js. It works quite well now.
I am however wondering if there is any way to improve my minimizing regex calls. Some ments are not striped from the CSS entirely, and I notice a few other hiccups here and there.
Also, considering my abilities with regex, I might be able to do the same in half the calls. :)
Any suggestions will be greatly appreciated.
Thanks.
function minimizeData( _content ) {
var content = _content;
content = content.replace( /(\/\*.*\*\/)|(\n|\r)+|\t*/g, '' );
content = content.replace( /\s{2,}/g, ' ' );
content = content.replace( /(\s)*:(\s)*/g, ':' );
content = content.replace( /(\s)+\./g, ' .' );
content = content.replace( /(\s|\n|\r)*\{(\s|\n|\r)*/g, '{' );
content = content.replace( /(\s|\n|\r)*\}(\s|\n|\r)*/g, '}' );
content = content.replace( /;(\s)+/g, ';' );
content = content.replace( /,(\s)+/g, ',' );
content = content.replace( /(\s)+!/g, '!' );
return content;
}
I have a strait forward aggregator/minimizer/cacher I've written in node.js. It works quite well now.
I am however wondering if there is any way to improve my minimizing regex calls. Some ments are not striped from the CSS entirely, and I notice a few other hiccups here and there.
Also, considering my abilities with regex, I might be able to do the same in half the calls. :)
Any suggestions will be greatly appreciated.
Thanks.
function minimizeData( _content ) {
var content = _content;
content = content.replace( /(\/\*.*\*\/)|(\n|\r)+|\t*/g, '' );
content = content.replace( /\s{2,}/g, ' ' );
content = content.replace( /(\s)*:(\s)*/g, ':' );
content = content.replace( /(\s)+\./g, ' .' );
content = content.replace( /(\s|\n|\r)*\{(\s|\n|\r)*/g, '{' );
content = content.replace( /(\s|\n|\r)*\}(\s|\n|\r)*/g, '}' );
content = content.replace( /;(\s)+/g, ';' );
content = content.replace( /,(\s)+/g, ',' );
content = content.replace( /(\s)+!/g, '!' );
return content;
}
Share
Improve this question
asked Dec 9, 2010 at 19:27
SpotSpot
8,1559 gold badges48 silver badges55 bronze badges
1 Answer
Reset to default 13function minimizeData( _content ) {
var content = _content;
content = content.replace( /\/\*(?:(?!\*\/)[\s\S])*\*\/|[\r\n\t]+/g, '' );
// now all ments, newlines and tabs have been removed
content = content.replace( / {2,}/g, ' ' );
// now there are no more than single adjacent spaces left
// now unnecessary: content = content.replace( /(\s)+\./g, ' .' );
content = content.replace( / ([{:}]) /g, '$1' );
content = content.replace( /([;,]) /g, '$1' );
content = content.replace( / !/g, '!' );
return content;
}
should be a bit clearer and avoids repetition. After the first replace, there will only be spaces left; after the second replace, only single spaces. This makes the following replaces easier.
To explain the ment-removing regex (shown here as a pure verbose regex without delimiters):
/\* # Match /*
(?: # Match (any number of times)...
(?!\*/) # ... as long as we're not right before a */:
[\s\S] # any character (whitespace or non-whitespace).
)* # (End of repeated non-capturing group)
\*/ # Match */
本文标签: javascriptRegEx to minimize CSSStack Overflow
版权声明:本文标题:javascript - RegEx to minimize CSS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741497891a2381916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论