SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Threaded View

  1. #1
    SitePoint Zealot
    Join Date
    Oct 2007
    Bilthoven, Netherlands
    0 Post(s)
    0 Thread(s)

    Trouble removing css comments with RegExp

    I want to remove comments from css files using nodejs. I have this working fairly well:
    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:
    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:
    /*background:transparent 0 0 no-repeat url('/grontmij/img/close_button.png');*/
    I could get rid of everything with this simpler one:
    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.
    Attached Files Attached Files


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts