我正在使用 vsc,我想使用零填充将找到的任何数字转换为固定的 4 位数字长度。例如 : 2 -> 0002 99 -> 0099
我可以使用查找和替换吗?或任何扩展名?
谢谢
如果您想更改文件中的数字,您可以使用我编写的扩展Regex Text Generator
Ctrl+F
.*
按钮\d+
Alt+Enter
(.*)
{{N[1]:size(4)}}
Return
接受,或者 Escape
使用我编写的扩展查找和转换非常容易完成。
进行此键绑定(在您的
keybindings.json
中):
{
"key": "alt+p", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "\\b(\\d{1,4})\\b",
"isRegex": true,
"replace": [ // replace with some javascript computation
"$${",
// $1 is the first capture group from the find regex
"return '$1'.padStart(4, '0');",
"}$$",
],
"postCommands": "cancelSelection",
}
}