Home Find CSS class names that are not inside comments using Regular Expressions
Reply: 0

Find CSS class names that are not inside comments using Regular Expressions

user6583
1#
user6583 Published in September 21, 2018, 8:05 am

I'm trying to replace all class names in a CSS file. I am using JavaScript/Node.js.

My current solution is this: /\.[a-z][a-z0-9-_]*/g. At the end of the file is a comment that references the source map file: /*# sourceMappingURL=style.css.map */. Now the file extensions of that URL also get replaced.

Given the following input:

.text-center { text-align: center; }
table.simple{background:#fff;}.bg-white{background:#fff;}
/*# sourceMappingURL=style.css.map */
/*
Example comment file.css
*/

the result is:

.a { text-align: center; }
table.a{background:#fff;}.a{background:#fff;}
/*# sourceMappingURL=style.a.a */
/*
Example comment file.a
*/

what I want is:

.a { text-align: center; }
table.a{background:#fff;}.a{background:#fff;}
/*# sourceMappingURL=style.css.map */
/*
Example comment file.css
*/

How do I have to change my RegEx so it does only match class names, that are outside of comments (// comments are not relevant here)?

share|improve this question
  • Which regex engine/language are you using? – ctwheels Feb 12 at 19:35
  • I am using JavaScript/Node.js @ctwheels. – mbrandau Feb 12 at 19:49

4 Answers 4

active oldest votes
up vote 1 down vote accepted
+100
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.476768 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO