在VS Code中找到以给定短语开头和结尾的所有位置,并在每次出现的开头添加一个特殊符号

时间:2018-11-30 13:29:13

标签: regex visual-studio-code

让我们从一个例子开始。我想找到整个解决方案中所有以词组template开头并以.js结尾的地方。 例如 匹配:

template\blah\loadprojdata.js
template\scripts\showhidecontrols.js
template\scripts\pageloader.js
template\scripts\andsearchfield.js

不匹配:

template\scripts\pageloader.css
\template\scripts\andsearchfield.js

对于所有出现的事件,我想在开头添加一个\符号,以便预期结果如下:

\template\blah\loadprojdata.js
\template\scripts\showhidecontrols.js
\template\scripts\pageloader.js
\template\scripts\andsearchfield.js

到目前为止,我已经创建了一个表达式

^template.*\.js$

但是我不知道如何将\添加到找到的每一行的开头。我在VS代码中只有“替换”选项。有任何想法吗? 干杯

1 个答案:

答案 0 :(得分:2)

您可以使用替换

查找:^(template.*\.js)$
替换:\\$1

(template.*\.js)是一个ID为1的捕获组,其值由替换模式中的$1占位符引用。要用文字\代替,应加倍。

enter image description here

enter image description here