Visual Studio Code 使用什么类型的正则表达式? [英] What flavor of Regex does Visual Studio Code use?

查看:37
本文介绍了Visual Studio Code 使用什么类型的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 Visual Studio Code 中搜索替换,我发现它的 Regex 风格与完整的 Visual Studio 不同.具体来说,我尝试使用 string (?<p>[\w]+) 声明一个命名组,它在 Visual Studio 中有效,但在 Visual Studio Code 中无效.它会抱怨错误 Invalid group.

除了解决这个特定问题之外,我还在寻找有关 Visual Studio Code 中正则表达式风格的信息以及在哪里可以找到有关它的文档,以便我可以帮助自己解决我可能遇到的任何其他问题.

完整的 Visual Studio 使用 .NET 正则表达式,如

在文件小部件中查找/替换中的 JavaScript 正则表达式

测试差异

在文件侧边栏中查找不支持 (?=foobar) 而在文件小部件中查找确实支持该先行语法.

关于使用组查找/替换

要查找/替换为组,请使用括号() 来分组和$1$2$3, $n 替换.

这是一个例子.

之前:

之后:

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.

解决方案

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.

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.

Test the Difference

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

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:

After:

这篇关于Visual Studio Code 使用什么类型的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆