VsCode正则表达式批量替换

357 阅读1分钟

示例代码:

<a href="http://www.xxoo.com">关键词一</a>
<a href="http://www.xxoo.com" title="xxoo">关键词二</a>
<a href="http://www.xxoo.com" target="_blank">关键词三</a>

1. .匹配任何字符

查找输入 关键..
替换输入 关键25
结果:
<a href="http://www.xxoo.com">关键25</a>
<a href="http://www.xxoo.com" title="xxoo">关键25</a>
<a href="http://www.xxoo.com" target="_blank">关键25</a>

2. .*匹配以xx开头或结尾的所有

查找输入 .*关
匹配到的是 <a href="http://www.xxoo.com">关
查找输入 关.*
匹配到的是 关键词一</a>

删除所有console.log

查找输入 [^]*console.log[^]*
删除空行 ^\s*(?=\r?$)\n
删除单行注释 //[\s\S]*?\n
删除块注释 /\*(.|\r\n|\n)*?\*/
删除所有注释 \/\*[\s\S]*\*\/|\/\/.*

批量添加内容

13100002222
13100003333
13100004444
13100005555
13312345678
13987654321
查找输入 \d+
替换输入 "$0"
结果为
"13100002222"
"13100003333"
"13100004444"
"13100005555"
"13312345678"
"13987654321"

查找输入 (\d{3})(\d{4})(\d{4})
替换输入 $1-$2-$3
结果为
131-0000-2222
131-0000-3333
131-0000-4444
131-0000-5555
133-1234-5678
139-8765-4321

批量替换文件路径

<link rel="stylesheet" href="static/cui.css">
<link rel="stylesheet" href="static/style.css">
<link rel="stylesheet" href="static/less.css">

查找:"static/(.*).css"
替换:"static/css/$1.css"

结果:

<link rel="stylesheet" href="static/css/cui.css">
<link rel="stylesheet" href="static/css/style.css">
<link rel="stylesheet" href="static/css/less.css">

从标签中取出文字

<a href="http://www.xxoo.com">关键词一</a>
<a href="http://www.xxoo.com" title="xxoo">关键词二</a>
<a href="http://www.xxoo.com" target="_blank">关键词三</a>

查找:"<a href="(.)>(.)"
替换:"$2" 结果:

关键词一
关键词二
关键词三

替换:"$1"

http://www.xxoo.com"
http://www.xxoo.com" title="xxoo"
http://www.xxoo.com" target="_blank"