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.
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:
<h1>(.+?)</h1>
#### $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 characterAnd 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!
I am terrible at optimizing my keyboard layout for anything. But off lately, my little…
I recently switched completely to the Brave browser and have set ad blocking to aggressive…
I was preparing a slide deck for a hackathon and decided to put in a…
I have been using npx a lot lately, especially whenever I want to use a…
Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…
While working on a project, I wanted to do an integrity check of a file…