regex - Is there a way to find all occurrences of using var with a require, and replacing just those results with const?
问题描述
In VS Code, is there a way to use a regular expression to find all statements which use var
to declare a variable that assigns a value using require
, and then replace just the var
with const
?
I have tried searching and I’m unable to find an answer.
This is the attempt at the regex (this part works): var[ 0-9a-zA-Z]*= require
It is the search and replace portion of just a part of the match where I am unsure how to do it, or if it is possible.
I’ve set up a StackBlitz.
In this screenshot, I’m trying to replace
var zlib = require('zlib');
by
const zlib = require('zlib');
解决方案
in find section: var(.*?)=(.*?)require\(
in replace section: const$1=$2require(
Using round brackets around regex (.*) allows it to become a group which can be referred in the replace section of vscode by using group number $1,$2 and so on
Please read vscode documentation here for detailed explanation
Please read this answer for another example.
推荐阅读
- mysql - 是否有一个 MySQL 查询来查找多个字段的最佳匹配?
- php - 如何将html href按钮放在php代码中
- gradle - 使实现依赖在另一个子模块中可用
- python - Python端口扫描器在使用for循环和范围函数时将每个端口标记为关闭
- signal-processing - 负幅值的快速傅里叶变换
- python-3.x - 读取文件并计算python中列中的单词
- angular - 如何根据一个select选项确定剩下的select需要绑定的值?
- c - 使用管道,从父进程中读取 2 个数字,子进程计算它们的总和并为父进程提供打印结果
- mongodb - 在Mongodb中,主键(IDHACK)和辅助键(IXSCAN)的工作流程有什么区别?
- html - 如何在网站中使用 html 和 css 使用没有背景的图像(即使它不应该有白色背景)