I want to remove comments from css files using nodejs. I have this working fairly well:
Code:
String.prototype.clean = function(){
return this.replace(/(\n|\r|\t|\f|\v)+/g,'');
};
S = S.clean(); // Remove line breaks, tabs etc.
var e1 = new RegExp("/[*][^*/]*[*]/",'g'); // Remove comments.
S = S.replace(e1,r1);
var e2 = new RegExp("/[*][^/]*[*]/",'g'); // Remove remaining comments.
S = S.replace(e2,r1);
S = S.replace(/ +/g,' '); // Remove redundant whitespace.
S = S.replace(/ *{ */g,'{'); // Remove spaces around {
S = S.replace(/ *} */g,'}'); // Remove spaces around }
S = S.replace(/ *: */g,':'); // Remove spaces around :
S = S.replace(/ *; */g,';'); // Remove spaces around ;
The e1 above fails to remove this comment:
Code:
/*********************************/
Hence I made e2 to get tid of that.
But I still have one comment that I can't get rid of, it looks like this:
Code:
/*background:transparent 0 0 no-repeat url('/grontmij/img/close_button.png');*/
I could get rid of everything with this simpler one:
Code:
new RegExp("/[*].*[*]/",'g');
But that becomes a greedy match and removes everything (including code) between the first and the last comment in the code.
I am sure that with the right tweaking, e1 should be able to fix all comments. What I am trying to say to it is:
Match everything except */ between /* and */
- but I am obviously failing to do so.
Any ideas?
See attached CSS files.
Bookmarks