VScode Regex

Mastering search and replace regex in Visual Studio Code

Published: Wednesday, Dec 6, 2023 Last modified: Monday, Dec 9, 2024

Imagine a file with comments that you needed to remove:

"Remove this silly comment please"
keepme()

To match content between the quote and remove it, you could use this regex:

"(.*)"\n

But what if the comment was multiline?

keepme()
"""
Remove this silly comment please
and this one too
"""
alsokeepme()

You could use this regex:

"""(.|\n)*"""\n   

You can do a lot more with regex in VS Code, like reusing what’s matched and changing case.

VS Code also has Multiline search input, which is useful for matching content across multiple lines.

Visual Studio Code uses the PCRE2 regex engine and you can test your regex here. However for workspace searches ripgrep/rust regex engine is used, and if the regex fails it will fall back to PCRE2.