如何在 vsc 中将数字转换为补零格式?

问题描述 投票:0回答:2

我正在使用 vsc,我想使用零填充将找到的任何数字转换为固定的 4 位数字长度。例如 : 2 -> 0002 99 -> 0099

我可以使用查找和替换吗?或任何扩展名?

谢谢

visual-studio-code padding digits
2个回答
0
投票

如果您想更改文件中的数字,您可以使用我编写的扩展Regex Text Generator

  • 打开查找:
    Ctrl+F
  • 启用正则表达式搜索:
    .*
    按钮
  • 搜索:
    \d+
  • 全选(焦点位于查找对话框中):
    Alt+Enter
  • 执行命令:根据正则表达式(regex)生成文本
  • 使用匹配正则表达式:
    (.*)
  • 使用生成器正则表达式:
    {{N[1]:size(4)}}
  • 如果您看到自己喜欢的内容,请点击
    Return
    接受,或者
    Escape

0
投票

使用我编写的扩展查找和转换非常容易完成。

进行此键绑定(在您的

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",
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.