通过 "Find and replace" in VSCode 删除大括号

Remove curly brackets via "Find and replace" in VSCode

我需要在导入组件名称时删除括号:

示例字符串:

import { Component } from 'components/components1

Find 字段中使用的正则表达式:

import (\{(.*?)\}) from 'components/(componentgroup|componentgroup2|componentgroup3)

替换字段中使用的模式:

import  from 'ui/

这样我可以select所有出现的地方,但无法在文本中得到预期的结果。

除了手动操作还有其他方法吗?

我也有这个正则表达式,没有括号的匹配:

([^{}\s](?:[^{}]*[^{}\s])?)

你的正则表达式很好,尽管你可以在末尾写一个单词边界以确保你将组件作为整个单词匹配:

import (\{(.*?)\}) from 'components/(componentgroup|componentgroup2|componentgroup3)\b

接下来,您的替换图案必须是

import  from 'ui/

第 2 组在前,然后是第 3 组。

参见regex demo

加强版:

import\s+\{\s*(.*?)\s*\}\s+from\s+'components\/(componentgroup|componentgroup2|componentgroup3)\b

替换为import from 'ui/

this regex demo

由于 \{\s*(.*?)\s*\} 中的 \s* 包名称将从空格中删除,并且 \s+ 将确保匹配将发生,即使它们之间有任何种类和数量的空格的话。注意 Visual Studio 代码正则表达式有点特殊,\s 不匹配换行符,除非模式中有 \r\n,因此,在这种情况下,\s不会越界