json - VSCode 片段,将由下划线分隔的小写字符串转换为 CamelCase?

标签 json regex visual-studio-code camelcasing vscode-snippets

我正在 中编写自定义代码段VSCode 帮助我轻松定义自定义类方法。我需要能够输入字符串 'formatted_like_this' 并让正则表达式在某些地方转换该字符串,使其成为 'FormattedLikeThis'?

要在 php.json 中编写的自定义代码段:(有关我正在努力的地方,请参阅此处的“需要使用正则表达式帮助”)

"New Custom Class Method For Variable": {
    "prefix": "contcmpffv",
    "body": [
        "protected $$1 = null;",
        "public function get${NEED HELP WITH REGEX HERE}()",
        "{",
        "\t$0",
        "}"
    ],
    "description": "Controller Class Method Public Function For Variable"
}

我想要的工作流程:
1. 输入 contcmpffv
2. 在提示匹配代码段时按 Enter
2. 代码片段提示我 1 美元

所需的输出(在提示输入 $1 时输入“test_input_string”):
protected $test_input_string = null;
public function getTestInputString()
{
    *cursor resolves here (due to $0)*
}

最佳答案

尝试:

"body": [
    "protected $$1 = null;",
    "public function get${1/(.*)/${1:/pascalcase}/}()",
    "{",
    "\t$0",
    "}"
],

它使用未记录的 pascalcase转换 - 已经存在一段时间了。在这种情况下,它会为您完成所有工作。

如果没有 pascalcase,这就是你可以使用的。 :
"public function get${1/([^_]*)_*/${1:/capitalize}/g}()",

关于json - VSCode 片段,将由下划线分隔的小写字符串转换为 CamelCase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144915/

相关文章:

ios - 如何修改包含 '-'作为单词字符的单词边界

javascript - 正则表达式用重复模式替换一行

angular - Angular2+ 项目中缺少 .vscode 文件夹

json - 如何按市值获取 coingecko API 的前 300 个硬币的列表?

regex - 排除 OCaml RegEx 中的特定单词

visual-studio-code - VS代码: Autofill (in Scala/Metals) forces the cursor to jump *after* the parentheses

node.js - 如何在 Node+Typescript+VSCode 中查找缺少的 Await on Async 函数调用?

json - Angular 4 从数组对象中删除重复项

javascript - 为 Highcharts 圆环图转换组 json

json - 通过多个编码保留 json.RawMessage