Preg Expression to identify classes/ids in a CSS file that have no contents

Posted by dclowd9901 on Stack Overflow See other posts from Stack Overflow or by dclowd9901
Published on 2010-03-16T20:12:08Z Indexed on 2010/03/16 20:21 UTC
Read the original article Hit count: 459

Filed under:
|
|

I'm in the process of updating some old CSS files in our systems, and we have a bunch that have lots of empty classes simply taking up space in the file. I'd love to learn how to write Regular expressions, but I just don't get them. I'm hoping the more I expose myself to them (with a little more cohesive explanation), the more I'll end up understanding them.

The Problem

That said, I'm looking for an expression that will identify text followed by a '{' (some have spaces in between, and some do not) and if there are no letters or numbers between that bracket and '}' (spaces don't count), it will be identified as a matching string.

I suppose I can trim the whitespace out of the doc before I run a regular expression through it, but I don't want to change the basic structure of the text. I'm hoping to return it into a large <textarea>.

Bonus points for explaining the characters and their meanings, and also an expression identifying lines in the copy without any text or numbers, as well. I will likely use the final expression in PHP script.

tl;dr:

Regular Expression to match:

.a_class_or #an_id {
    /* if there aren't any alphanumerics in here, 
       this should be a matching line of text */
}

© Stack Overflow or respective owner

Related posts about css

Related posts about php