Learn how Visual Studio Code (VSCode) implements regular expressions (regex) to help you perform your complex pattern-matching duties more effectively. In addition, I will introduce a few useful regex extensions to VSCode.
Latest posts by Timothy Warner (see all)

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.

VSCode includes two search experiences

VSCode includes two search experiences

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.

Include .gitignore file content in your VSCode search results

Include .gitignore file content in your VSCode search results

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:

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.

0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account