Using RegEx groups for Search & replace  in VS Code

Using RegEx groups for Search & replace in VS Code

Share

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!

0
Would love your thoughts, please comment.x
()
x