How to select through regex all class designations in css file

Having a css file with css rules, I would like to select only the css class (i.e.) .tblGenFixed

, but not the css values ​​for the rule (i.e.) opacity: 0.3

.

This is my regex: /(\.([\w_]+))/g

This is my alternative solution but it doesn't work /(?!\{)(\.([\w_]+))(?!\})/g

I gave an example in regex101 here https://regex101.com/r/gG4nN4/1

How to ignore CSS rules values?

+3


source to share


2 answers


See: What characters are valid in CSS class names / selectors?

The value after the digit will have a digit. Fortunately, valid CSS class names cannot start with a number :)

Your regex must match dot first, then letter or - or _

! if you're looking for spaces before point, a value like .5 will match ...

Try the following: (\.([a-zA-Z_-]{1}[\w-_]+))



Edit :

See also: Regex to match CSS class name

-?[_a-zA-Z]+[_a-zA-Z0-9-]*

Relevant quote:

Basically, a name must start with an underscore (_), a hyphen (-), or a letter (az), followed by any number of hyphens, underscores, letters, or numbers. There is a catch: if the first character is a hyphen, the second character must be a letter or underscore, and the name must be at least 2 characters long.

+4


source


Depending on how your CSS is written, you might get what you are looking for by requiring spaces before the period:

 \W/(\.([\w_]+))/g

      

Here's a fork of your regex .



Depending on what you are looking for, you can skip one of these capture groups:

\W\.([\w_]+)

      

I would also caution against parsing CSS with a regex without manually examining the results.

+2


source







All Articles