-2

I have a huge CSS file and I want to convert all ID selectors to Class selectors from a certain name using regEx in Find and Replace area in the text editor. I am using Sublime Text 3.

In this file, I have some hex colors as expected like #333 and #ff0000 which need to be considered and the regEx pattern should not track it.

Expected Result

find:

#333 /*do not track*/
#ff0000 /*do not track*/
#item1 /*found*/
#item2 /*found*/
#item3 /*found*/

replace:

#333 /*do not track*/
#ff0000 /*do not track*/
.item1 /*done!*/
.item2 /*done!*/
.item3 /*done!*/

P.S the name either can or not having numbers on it. Need to accept letters and numbers in a way that do not match this hex color pattern above

What is the best regex pattern to reach this? Thanks in advance!

Machavity
  • 30,841
  • 27
  • 92
  • 100
Luis Febro
  • 1,733
  • 1
  • 16
  • 21
  • 2
    Why don't you use a CSS parser instead of regex? There could be some element with id `ff0000`. – user202729 Aug 17 '19 at 00:19
  • This is just for practing my regExs skill sake. I did not hear about CSS parser. it is good to have alternatives. I made a research on it and found [this another question on CSS parser for JS](https://stackoverflow.com/questions/10963997/css-parser-for-javascript). They suggest `PostCSS` as a more recent solution. I will take a closer analysis on it. Thanks for the tip! – Luis Febro Aug 17 '19 at 00:45

1 Answers1

0

My guess is that maybe, find with,

#(?=item\d)

or

#(?=item)

and replace with a ..


If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


Demo 2

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    That did the work with #item+number. But if I for example have only the name without any number (#item) it does not work. Nonetheless, thanks to your further suggestions, I could even find a complementary solution: `#(?=item[\d]?)` for either with or without a number attached to the name. By the way, regEx debugger is something new to me and it rocks. Thanks! – Luis Febro Aug 17 '19 at 01:29