What flavor of Regex does Visual Studio Code use?

RegexReplaceVisual Studio-Code

Regex Problem Overview


Trying to search-replace in Visual Studio Code, I find that its Regex flavor is different from full Visual Studio. Specifically, I try to declare a named group with string (?<p>[\w]+) which works in Visual Studio but not in Visual Studio Code. It'll complain with the error Invalid group.

Apart from solving this specific issue, I'm looking for information about the flavor of Regexes in Visual Studio Code and where to find documentation about it, so I can help myself with any other questions I might stumble upon.

Full Visual Studio uses .NET Regular Expressions as documented here. This link is mentioned as the documentation for VS Code elsewhere on Stackoverflow, but it's not.

Regex Solutions


Solution 1 - Regex

Rust Regex in the Find/Replace in Files Sidebar

Rob Lourens of MSFT wrote that the file search uses Rust regex. The Rust language documentation describes the syntax.

Rob Lourens on GitHub

JavaScript Regex in the Find/Replace in File Widget

Alexandru Dima of MSFT wrote that the find widget uses JavaScript regex. As Wicktor commented, ECMAScript 5's documentation describes the syntax. So does the MDN JavaScript Regular Expression Guide.

Alexandru Dima on GitHub

Test the Difference

The find in files sidebar does not support (?=foobar) whereas the find in file widget does support that lookahead syntax.

Shows a lookahead working in the widget but not in the sidebar.

Regarding Find/Replace with Groups

To find/replace with groups, use parentheses () to group and $1, $2, $3, $n to replace.

Here is an example.

Before:

This is the text before the replace.

After:

This is the text after the replace.

Solution 2 - Regex

Shaun's answer is still correct, however to add an update, recently VS Code added the option to opt into using the Perl based PCRE2 engine. You can enable this through your settings config.

This allows you to perform more advanced regex operations like lookaheads and backreferences. But as noted below, the regex still has to be valid JavaScript regex.

> VS Code does support regular expression searches, however, > backreferences and lookaround aren't supported by default. But you can > enable these with the setting search.usePCRE2. This configures ripgrep > to use the PCRE2 regex engine. While PCRE2 supports many other > features, we only support regex expressions that are still valid in > JavaScript, because open editors are still searched using the editor's > JavaScript-based search.

And for a bonus if you ended up here trying to do multi line searches, VS Code recently added that feature as well!

enter image description here

Solution 3 - Regex

I've found newer information (July 22, 2020) about it.

IllusionMH left the following comment in Github:

> ripgrep (compatible with PCRE2) is already used for Find in files > functionality (for not open editors) and JS engine used only for open > editors.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEricView Question on Stackoverflow
Solution 1 - RegexShaun LuttinView Answer on Stackoverflow
Solution 2 - RegexprotoEvangelionView Answer on Stackoverflow
Solution 3 - Regexv.slobodzianView Answer on Stackoverflow