在 VsCode 中将 () 交换为 {} 的最有效方法? [英] Most efficient way to swap () for {} in VsCode?

查看:30
本文介绍了在 VsCode 中将 () 交换为 {} 的最有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio Code,并且经常发现自己不得不用大括号 {} 或副括号替换括号 ()反之.到目前为止,除了手动退格和替换每个字符之外,我还没有找到其他方法.

I'm using Visual Studio Code and often find myself having to replace parentheses (<code>) with curly brackets {<same code>} or vice versa. So far I haven't found a way to do this other than manually backspacing and replacing each character.

有没有更好的方法?

推荐答案

您可以在 VSCode 中使用正则表达式搜索和替换功能.

You can use the regex search and replace capability within VSCode.

将此作为您的搜索字符串:

Make this your search string:

(.*)(\()(.*)(\))

替换为:

$1{$3}

说明:

  1. 我们使用正则表达式捕获组来允许我们保留搜索字符串的某些部分,同时替换其他部分.

  1. We are using regex capture groups to allow us to retain some parts of the search string whilst replacing other parts.

第一个捕获组 (.*) 搜索并捕获第一个 ( 字符之前的任何内容.

The first capture group (.*) searches for and captures anything preceeding the first ( character.

第二个捕获组 (\() 获得左括号 - 我们将用 { 替换它.注意 (code> 被转义,因为 ( 是正则表达式中的运算符

The second capture group (\() gets the left parenthesis - which we will replace with a {. Notice that the ( is escaped, because ( is an operator in regex

第三个捕获组 (.*) 抓取括号内的任何内容 - 我们将再次将其放回.

The third capture group (.*) grabs whatever is inside the parentheses - we will put that back again.

最终捕获组 (\)) 获取 ) 字符,我们将其替换为 }

The final capture group (\)) grabs the ) char, which we swap out for a }

所以,$2$4 捕获组包含 () 字符,我们替换为文字 {} 字符.(如果我们想准确地放回我们找到的内容,我们会将:$1$2$3$4 作为替换文本)

So, $2 and $4 capture groups contain the ( and ) characters, which we replace with literal { and } characters. (If we wanted to put back exactly what we had found, we would put: $1$2$3$4 as the replace text)

重要提示:

注意(见下图)[.*] 按钮被点击(在搜索文本的右侧)

那个 [.*] 按钮使搜索成为正则表达式搜索.确保在完成后取消点击它.

That [.*] button makes the search a regex search. Make sure you unclick it when done.

不要点击全部替换按钮 - 一一进行替换.这个特定的正则表达式不会正确替换括号内的多行文本. 但是为了加快这个过程,鲍勃的叔叔.

Do not hit the Replace All button - do the replacements one-by-one. This particular regex will not correctly replace multi-line text inside the parens. But for speeding up the process, Bob's yer uncle.

在 RegEx101.com 进行演示

这篇关于在 VsCode 中将 () 交换为 {} 的最有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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