Shiran Dror Shiran Dror - 6 months ago 17
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/


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.