- Use Azure Bastion as a jump host for RDP and SSH - Tue, Apr 18 2023
- Azure Virtual Desktop: Getting started - Fri, Apr 14 2023
- Understanding Azure service accounts - Fri, Mar 31 2023
In this post, I assume you're already familiar with regular expression (regex) syntax, and you want to know more about how regex is implemented in VSCode. It turns out that this is quite an interesting story, with lots of conflicting information online.
Well, I chatted with some VSCode developers at Microsoft to make sure I understood how it really works under the hood. Allow me to share my distilled findings with you.
Two regular expression engines
As you know, VSCode includes two search experiences. There's the Search command, accessible from the Activity Bar, that searches all files in the current workspace. Then there's the document Find command, available from an open editor pane.
To make it clear, let's use the following terminology to describe VSCode's two search functions:
- Search: The magnifying glass icon on the Activity sidebar that searches all folders in the current workspace
- Find: Simple find/replace tool available for the current document only (accessible by clicking Edit > Find)
What you might not know is that VSCode uses multiple regex engines under the hood to ensure VSCode works the same, regardless of your operating system.
The workspace search in VSCode is powered by the open-source ripgrep line-oriented search tool. The ripgrep engine uses Rust regex under the hood. What's cool, though, is that if the ripgrep engine is unable to parse a regex, VSCode falls back to the Perl Compatible Regular Expressions version 2 (PCRE2) engine to process the result.
For example, ripgrep doesn't support backreferences and lookarounds, so if you use those in search, VSCode will invoke the PCRE2 engine automatically.
By contrast, the editor Find uses a JavaScript (specifically ECMAScript 5) regex engine that always supported backreferences and lookarounds. This distinction used to be much more important because customers were confused about why some regex patterns worked in the editor Find, but not the multifile Search. Nowadays, you can use Search and Find interchangeably, with no worry about regex incompatibility.
You will find an occasional behavior difference between the VSCode Search and Find engines. One of these differences has to do with line endings. Here's the difference:
- In the editor Find, you can match both CRLF and LF line endings by using the \n metacharacter
- In Search, \n matches only CRLF
Why not the .NET regex engine?
You might wonder, "Why doesn't Visual Studio Code use the .NET regex engine by default? After all, Microsoft makes .NET, Visual Studio Code, PowerShell, and so forth."
It's a good question, and the answer lies in these words: cross-platform compatibility. VSCode should work identically, regardless of whether you use the app on Windows, macOS, or Linux.
In case you're interested, .NET uses the Nondeterministic Finite Automation (NFA) regex engine. This engine is used by Emacs, Perl, Python, and Tcl, and is much faster than the Deterministic Finite Automation (DFA) engine used by regex-aware Linux tools like awk, egrep, and lex.
File exclusions—A potential point of confusion
By default, VSCode Search doesn't return search results that reference files ignored by your repository's .gitignore, .ignore, .rgignore, and /git/info/exclude files.
You can override this default behavior by disabling the files of the exclude filter in VSCode Search, as shown in the following screenshot.
The VSCode software engineers I spoke to told me that a common point of customer confusion concerns the files to exclude control. Specifically, you use glob patterns and not regular expressions to add your filter. In other words, to exclude all YML files from your search results, you would add.yml.
Useful VSCode regex extensions
Now that we understand how VSCode implements regular expressions, let's turn our attention to the Visual Studio Marketplace and regex-oriented extensions.
You'll find these extensions offer some regex-oriented convenience to make your development a bit easier. For instance:
- Regex Snippets: Easily insert regex patterns by using a text trigger
- Align by RegEx: Use a regex to apply alignment to multiple selected lines of text
- Regex Workbench: An in-app regex tester
- Regex Previewer: See regex matches in your target document in a separate editor pane
Something to keep in mind when testing VSCode extensions is that the extension developer might favor specific programming languages. For instance, the previously listed Regex Previewer extension offers a regex preview only for JavaScript, TypeScript, PHP, and Haxe code files.
Subscribe to 4sysops newsletter!
Wrap-up
I hope you now have a clearer picture of how Visual Studio Code implements regular expressions. If you're a developer, remember that because VSCode is an open-source project itself, you're welcome to fork and clone the project to inspect the source code and possibly submit your own bugfixes and new features via pull request.