VS Code

Using RegEx groups for Search & replace in VS Code

Advertisements

VS Code has a nifty tool for doing search and replace using RegEx, which I prefer using whenever doing bulk updates. It has saved me quite a few times while refactoring across multiple files. Let us look at how search & replace using RegEx groups works in Visual Studio Code.

Using RegEx in VS Code Search and replace

To use the search & replace feature in Visual Studio Code, we need to click on the .* icon, which can be found in the search input box. In the “Search” field, we can enter the pattern we want to search for.

We can also use parentheses () to create capture groups. These can then be referred to in the “Replace” field using $1$2, etc.

In the “Replace” input box, we can enter the text that we want to replace the matching text with. We can also use matches from the search section if we want to. $0 refers to the whole match; and $1, $2, etc., refer to the capture groups.

For example,

 instance to replace <h1>content</h1> to #### content within a document, we can use the following regex:

  • Search: <h1>(.+?)</h1>
  • Replace: #### $1

If you need it, following is a refresher on some special characters that can be used to match certain positions or conditions in the text, pretty standard RegEx items:

  • ^ to match the beginning of a line
  • $ to match the end of a line
  • | to match either one of 2 alternatives
  • ? to match 0 or one occurrence of the preceding character or group
  • * to match 0 or more occurrences of the preceding character or group
  • + to match 1 or more occurrences of the preceding character or group
  • {n} to match exactly n occurrences of the preceding character or group
  • {n,m} to match between n and m occurrences of the preceding character or group
  • [...] to match any one of the characters inside the brackets
  • [^...] to match any one of the characters not inside the brackets
  • \ to escape a special character and treat it as a literal character

And that is all there is to using RegEx groups for Search & replace in VS Code. What are your favorite RegEx patterns that you use? Do let us know in the comments below!

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

2 weeks ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

4 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

5 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

6 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

6 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

7 months ago
Advertisements