Search code examples
cssregexclasssublimetext3

Replacing all ID to Class with certain name using regEx in Sublime Text


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!


Solution

  • 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