VS Code Regex Search Recipes
Regular expressions for VS Code's Search view, including finding files that contain several words anywhere in the file.
On this page
VS Code's Search view (Ctrl+Shift+F, or Cmd+Shift+F on macOS) runs ripgrep under the hood. Turn on regular expressions with the .* button or Alt+R. This page collects patterns that need more than a simple search, and explains the multiline quirk that trips up most of them.
Find Files That Contain All of Several Words
To find files that mention every one of several words, in any order and on any line, chain lookaheads from the start of a line:
^(?=[\w\W]*word1)(?=[\w\W]*word2)Each (?=[\w\W]*wordN) checks that wordN appears somewhere after that point. The Search view then lists only files where all the lookaheads succeed. Add more (?=...) groups for more words, and wrap words in \b (like \bword1\b) to skip partial matches.
Why [\w\W] and Not [\s\S]
VS Code only switches ripgrep into multiline mode when the pattern contains \n, \r, or \W. The common "match anything" idiom [\s\S] contains none of these, so this pattern runs line by line:
^(?=[\s\S]*word1)(?=[\s\S]*word2)It only finds lines that contain both words. [\w\W] matches the same characters, including newlines, and its \W turns on multiline mode, so the lookaheads can see the whole file.
Find a Word Not Followed by Another
Find TODO comments that don't have an owner tag after them:
TODO(?!\s*\()Lookaheads and lookbehinds like this make VS Code use ripgrep's PCRE2 engine automatically. No setting is needed.
Use Capture Groups in Replacements
In the Replace field, refer to capture groups with $1, $2, and so on. For example, to swap lastName, firstName into firstName lastName:
(\w+), (\w+)$2 $1VS Code also supports case modifiers in replacements: \u$1 uppercases the first letter of the group, and \U$1 uppercases all of it.
Sources
This article is in the public domain (CC0 1.0), code samples included. Use it however helps you.