2 min read
Find and Replace with Regex Groups in VS Code

I sometimes need to search and replace across some code, but with parts of the search left untouched. Every time, I forget how to do it in VS Code.

The answer: $n.

For example, to convert every date from YYYY/MM/DD to YYYY-MM-DD:

search (with Regex activated):  (\d{4})\/(\d{2})\/(\d{2})  translation: (4 digits)/(2 digits)/(2 digits)                ↳group 1   ↳group 2   ↳group 3replace:  $1-$2-$3

Here’s how that looks in VS Code:

VS Code search and replace panel showing a regex search for dates in the format YYYY/MM/DD and replacing them with YYYY-MM-DD. Four matches are found across multiple files, with changes previewed in red (original) and green (replacement).