VS Code中批量将JS枚举转换为大写的方法
批量将JS枚举转换为大写的方法
方法1:VS Code正则批量替换(最便捷)
- 按下
Ctrl+H(Windows/Linux)或Cmd+H(Mac)打开替换面板 - 点击面板右上角的「正则表达式」开关(图标为
.*) - 「查找内容」输入:
^([a-z]+)$(若枚举名包含下划线,可调整为^([a-z_]+)$) - 「替换为」输入:
$1,点击替换框右侧的「AB」按钮(转换为大写),或直接输入\U$1\E - 点击「替换全部」完成批量转换
方法2:JS脚本批量处理
- 在VS Code终端(快捷键
Ctrl+)创建临时脚本convert-upper.js,写入以下代码:
const fs = require('fs'); const filePath = './your-enum-file.js'; // 替换为你的枚举文件实际路径 fs.readFile(filePath, 'utf8', (err, data) => { if (err) throw err; const convertedContent = data.replace(/^([a-z]+)$/gm, match => match.toUpperCase()); fs.writeFile(filePath, convertedContent, 'utf8', () => console.log('枚举已全部转换为大写')); });
- 运行脚本:
node convert-upper.js - 处理完成后可删除该临时脚本
方法3:多行编辑快速转换
- 按住
Alt键,鼠标点击所有枚举名的行首,生成多行光标 - 选中所有枚举名(或按
Ctrl+L选中整行) - 按下
Ctrl+Shift+U(Windows/Linux)或Cmd+Shift+U(Mac)直接转换为大写
内容的提问来源于stack exchange,提问作者Reco 4
相关产品推荐
相关产品推荐

