Skip to main content

Regex for searches in VSCode

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Visual Studio Code (VS Code) provides a powerful, built-in regex engine for its Find and Search functions. This feature allows you to perform highly specific and complex searches and replacements across a single file or an entire project.

How to Use Regex in VS Code

To enable regex in VS Code's search, you first need to activate the regular expression mode.

  1. Open the Find widget by pressing Ctrl + F (Windows/Linux) or Cmd + F (macOS) for a single file, or the global Search panel with Ctrl + Shift + F (Windows/Linux) or Cmd + Shift + F (macOS) to search across your project.
  2. Click the .* icon in the search bar. This icon toggles regex mode. When enabled, the icon will appear highlighted.

Once regex mode is active, you can use any valid regular expression to find patterns in your code.


Essential Regex Syntax

Here are some of the most common and useful regex patterns for searching and replacing in VS Code.

Character(s)DescriptionExample
.Matches any single character except a newline.a.b matches aab, axb, a1b.
*Matches zero or more of the preceding character.a* matches a, aa, or an empty string.
+Matches one or more of the preceding character.a+ matches a, aa, aaa, but not an empty string.
?Matches zero or one of the preceding character.colou?r matches color and colour.
^Start of a line.^import matches lines that begin with import.
$End of a line.} matches } only at the end of a line.
[abc]Character set. Matches any single character within the brackets.[aeiou] matches any single vowel.
[^abc]Negated character set. Matches any character not within the brackets.[^0-9] matches any non-digit character.
`(....)`Alternation/OR. Matches either the expression on the left or the right.
\bWord boundary. Matches the position between a word character and a non-word character.\bword\b matches word but not wordy.
\dMatches a digit (0-9).\d+ matches one or more digits.
\sMatches a whitespace character (space, tab, newline).\s can be used to match multiple lines.

Find and Replace with Capture Groups

One of the most powerful features is using capture groups to rearrange or extract text. A capture group is created by wrapping part of your regex in parentheses (). The matched content is then "captured" and can be referenced in the "Replace" field.

  • To reference a capture group, use $1, $2, and so on, in the replace field, where the number corresponds to the group's position.
  • $0 refers to the entire matched string.

Practical Examples

GoalFind RegexReplace RegexDescription
Wrap a number in quotes(\d+)'$1'Finds one or more digits and wraps them in single quotes.
Reorder a date (YYYY-MM-DD to DD.MM.YYYY)(\d{4})-(\d{2})-(\d{2})$3.$2.$1Captures the year, month, and day and rearranges them.
Change HTML to Markdown<h1>(.+?)</h1># $1Finds an <h1> tag and replaces it with a Markdown heading, preserving the inner text.

For more advanced needs, you can also search for and replace multiline patterns using a regex like ([\s\S]+).

Sources

  1. VS Code Docs: Regular Expressions
  2. Microsoft Learn: Using regular expressions in Visual Studio
  3. Medium: VS Code Regex Search

A video demonstrating how to find and replace with regex in VS Code might be useful for a visual walkthrough. vscode | regex find and replace.