Advanced Search Replace in Visual Studio Code with RegEx

One of the most useful features in Visual Studio Code (VS Code) is its search and replace tool. It allows you to quickly find and update text in your files. For even more control, the search tool also supports Regular Expressions (RegEx) to search using patterns instead of plain words. This can help you make big changes faster and more accurately.

Let’s see how it works.


Enabling RegEx

To begin, open the “Search” view in VS Code by pressing Ctrl+Shift+F (Windows/Linux) or Cmd+Shift+F (macOS). Alternatively, you can access it via the left sidebar.

Within the “Search” view, you’ll see a search input field and a replace input field. To enable RegEx, click the .* icon (the “Use Regular Expression” button) to the right of the search input field. This icon will highlight, indicating that RegEx is now active.

Enable RegEx in VSCode Search

Now you’re ready to start using RegEx. Let’s see some examples.


Uppercase to Lowercase

One common scenario is needing to change the case of characters.

Let’s say you have quite a number of JSON files where one of the property values is in uppercase letters, for example: "trip_flight_airline": "SQ". Since you have multiple files, changing them one by one wouldn’t be practical. This is where search and replace with RegEx comes in handy.

However, if the requirement changes and the value needs to be in lowercase, for example: "trip_flight_airline": "sq", you could do:

Search: ([A-Z]+)

This will match any sequence of uppercase letters from A to Z.

Replace: \L$1

This will replace the entire matched string, and using the special pattern \L, it will replace the matched string with its corresponding lowercase characters.

Converting Text to Lowercase VSCode

Lowercase to Uppercase

Similarly, if you need to change the case from lowercase to uppercase, you can do the following:

Search: ([a-z]+)

This will match any single lowercase letter from a to z.

Replace: \U$1

This will replace the entire matched string and replace it with its corresponding uppercase characters.

Changing the character case can be useful in many scenarios, such as when you need to standardize the case of property names or values in your code.


Capturing Groups and Reordering Text

RegEx can become really powerful when you use capturing groups which allow you to grab parts of the matched text and then rearrange them however you like.

For example, let’s say you have dates written like this: 07-15-2025 (month-day-year), and you want to change them to this format: 2025/07/15 (year/month/day).

You can do this in VS Code’s search and replace using the following pattern:

Search: (\d{2})-(\d{2})-(\d{4})

This will match any date in the format of two digits for the month, two digits for the day, and four digits for the year, separated by hyphens.

Replace: $3/$1/$2

This will rearrange the matched groups so that the year comes first, followed by the month and day, separated by slashes.

Date Format Conversion Using RegEx

Transforming snake_case to camelCase

Converting from snake_case, like my_variable_name, to camelCase, like myVariableName, is a common task when cleaning up or refactoring code.

If your variable names start with a dollar sign ($), you can use RegEx in VS Code to do the search replace more efficiently.

Search: (\$[^_\s\$\-\[]*?)_([a-z])

This will match any variable that starts with a dollar sign, followed by any characters except underscores, spaces, dollar signs, or square brackets, and then an underscore followed by a lowercase letter.

Replace: $1\U$2

Here we combine the matched variable name with the second part of the match, which is the lowercase letter after the underscore, and convert it to uppercase using \U.

Snake Case to Camel Case

This will effectively transform $my_variable into $myVariable. However, since VS Code doesn’t support variable-length lookbehind, it won’t match variables that have more than one underscore, like $my_variable_name. In such cases, you’ll need to run the search and replace multiple times to handle each underscore separately.


Wrapping up

In this article, we’ve explored how to use RegEx in Visual Studio Code’s search and replace feature to perform advanced text transformations, from changing character cases to reordering text and converting variable naming conventions.

Using RegEx in Visual Studio Code’s search and replace feature can significantly speed up your workflow, especially when dealing with large codebases or repetitive tasks.

By mastering RegEx, you can quickly make complex changes across multiple files without the need for manual edits.

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail