Shiran Dror Shiran Dror - 1 year ago 149
Javascript Question

get all classes from css file (as text) using regex

I'm working on a regex that will return all available classes from a css file. so far i have this:


this works fine but it also captures some css rules. Example:

.sky-form .button {
background-color: #2da5da;
opacity: 0.8;
transition: opacity 0.2s;
-o-transition: opacity 0.2s;
-ms-transition: opacity 0.2s;
-moz-transition: opacity 0.2s;
-webkit-transition: opacity 0.2s;

it will return ".sky-form" ".button" and ".2s". how can I prevent the regex from returning anything from between the curly brackets?

Edit - final answer


the last part (?:[\s.{>#:]{1}) is to make sure its not capturing any image links like .png or part of paths like jquery.11.1.custom/

Answer Source

You need to tell it not to allow a digit as a first character after the initial period.


The regex above allows for a class name that starts with a-z of both upper and lower case, or an underscore.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download