Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5970057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:19:11+00:00 2026-05-22T20:19:11+00:00

I am trying to remove specific CSS selectors, and if there is no more

  • 0

I am trying to remove specific CSS selectors, and if there is no more selectors for a list of properties than the script removes it…

I was able to get a part of the script working: http://jsfiddle.net/jnbdz/MarRr/5/

Here is the code:

$$('button')[0].addEvent('click', function(){

var css = document.id('css').get('text');

var newCss = css.replace(/(\n|\r|\s\s)/g, "")
.replace(/(,\s*body\s*(?={)|,\s*body\s*(?=,)|body\s*,|,\s*head\s*(?={)|,\s*head\s*(?=,)|head\s*,)/gi, "")
.replace(/(body\s*(?={)|head\s*(?={))/gi, "")
.replace(/(^\{[^}]*?\}|\}\s*\{[^}]*?\})/gim, "");

document.id('cleancss').set('text', newCss);

});

The problem is that if I remove the line breaks the script I wrote wont be able to remove the properties that are not related to any selectors…

If I keep the line breaks it works…

Also, I would like to know from coders that are good with ReGex if my code is good…

Thanks a lot in advance for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:19:12+00:00Added an answer on May 22, 2026 at 8:19 pm

    In the last replace you’re using the multiline flag. That can’t work, if you have only one line, which you do after the first replace. So lets keep the linebreaks first and remove them after the removal of the selectors.

    You also can simplify the regexes a bit. Use x(?=a|b) instead of x(?=a)|x(?=b). You also don’t need the lazy match [^\}]*?.

    Below is a working example. For clarity I only removed the body selector.

    $$('button')[0].addEvent('click', function(){
    
        var css = document.id('css').get('text');
    
        var newCss = css
            // replace multiple tabs or spaces by one
            .replace(/( |\t)+/g, " ")
            // remove space at beginning of line
            .replace(/(^\s)/gm, "")
            // remove body in selector lists
            .replace(/(,\s*body\s*(?={|,)|body\s*,)/gi, "")
            // remove body before {
            .replace(/(body\s*(?={))/gi, "")
            // remove rules without selector
            .replace(/[\n\r]+\{[^\}]*\}/g, "")
            // remove linebreaks
            .replace(/[\n\r]+/g, "");
    
        document.id('cleancss').set('text', newCss);
    
    });
    

    You could further compress the stylesheet by removing spaces in front of { or after : and ,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the following code to display only on specific pages but
I am trying to remove a specific user from sharepoint and have hit a
I'm trying to remove a specific node from a XmlNodeList named listaWidths. This specific
I am trying to remove a specific set of data from a MySQL database
I'm trying to remove a specific item from the ASP.NET Cache in order to
I'm working in wordpress, trying to figure out how to change the css color
I have a table, but it is not in a list format, meaning not
I'm tyring to style the jQuery datepicker. I can't use a jQuery theme as
Background: I'm using jquery blockui 2.31 on jquery 1.3.2, on ASP.NET 2.0. I'm using
I have some questions about focus in general (adhering to WC3 format, screw IE).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.