Using regex with find-and-replace in VS Code
The context for how and why is too niche for me to go in to here, but I had a bunch of files where I needed to change all instances of this:
Except for each instance, the id value (the number) was different.
I wanted to keep the number in there, but change the text that wraps it.
Because I needed to replace text on either side of the number, a simple find-and-replace term wasn’t going to work. I needed regex.
To turn on search using regex, you want to make sure this option is selected:
To replace the text on either side of text, you want a regular expression that follows this pattern:
AFTER_TEXT match the text on the left and the right of the text you want to wrap-replace.
Then, in the Replace input of the Search UI, you add a
$1 to wherever you want the original text to go.
For my case above, I used this:
Thanks for reading! If you'd like to share your thoughts you can leave a comment, send me an email, Tweet at me, or add an issue on GitHub.
The Comments system is powered by a third party service - Talkyard. Sometimes they don't load 😞. If you're having trouble leaving a comment you can send me an email.