English 中文(简体)
暂缓执行
原标题:RegEx to minimize CSS

我有一张海峡前座总汇/米吉斯/马舍尔。 它现在运作良好。

I am however wondering if there is any way to improve my minimizing regex calls. Some comments are not striped from the CSS entirely, and I notice a few other hiccups here and there.

此外,考虑到我的能力,我或许能够在半数电话中这样做。

任何建议都将受到高度赞赏。

感谢。

function minimizeData( _content ) {
    var content = _content;
    content = content.replace( /(/*.**/)|(
|
)+|	*/g,    );
    content = content.replace( /s{2,}/g,     );
    content = content.replace( /(s)*:(s)*/g,  :  );
    content = content.replace( /(s)+./g,   .  );
    content = content.replace( /(s|
|
)*{(s|
|
)*/g,  {  );
    content = content.replace( /(s|
|
)*}(s|
|
)*/g,  }  );
    content = content.replace( /;(s)+/g,  ;  );
    content = content.replace( /,(s)+/g,  ,  );
    content = content.replace( /(s)+!/g,  !  );
    return content;
}
最佳回答
function minimizeData( _content ) {
    var content = _content;
    content = content.replace( //*(?:(?!*/)[sS])**/|[
	]+/g,    );
    // now all comments, 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;
}

应当更加明确,避免重复。 第一次更换后,只有空位;第二次更换后,只有单一空位。 这使得以下替代更为容易。

解释取消评论的条例(此处为无限制的纯净ver):

/*       # Match /*
(?:       # Match (any number of times)...
 (?!*/)  # ... as long as we re not right before a */:
 [sS]   # any character (whitespace or non-whitespace).
)*        # (End of repeated non-capturing group)
*/       # Match */
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签