Sublime Text: Find and Replace Similar Items using Regex

It can be frustrating when you need to use find and replace on similar, but not identical, items and you find yourself wasting a lot of time doing them one-by-one. With a regex for Sublime Text, we can alleviate your frustration with this great time-saver.

We are going to use the following 3 lines in the image below as an example. Notice how the last class of each list item is different.

Sublime Text find and replace regex

First, you want to pull up Find and Replace (Option+CMD+F or CTRL+ALT+F, depending on your OS) and click the regex button on the top left of the box (where the red arrow is pointing above).

Sublime Text find and replace regex

Next, we will enter the regex in the ‘Find What’ input box. The original expression is this:

[^<]+

The regex we will use to replace the classes with one uniform class in this example is this:

class="[^<]+"

Once we enter the regex, we’ll move down to the ‘Replace With’ input box. We will give the list items a new class name:

class="feat-section"

Last, click the ‘Replace All’ button.

Sublime Text find and replace regex

The final result is above.