首页 > 解决方案 > VSCode片段,将下划线分隔的小写字符串转换为CamelCase?

问题描述

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

要在 php.json 中编写的自定义代码段:(请参阅“需要帮助 REGEX HERE”了解我遇到的问题)

"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)*
}

标签: jsonregexvisual-studio-codecamelcasingvscode-snippets

解决方案


尝试:

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

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

如果没有,这是您可以使用的pascalcase

"public function get${1/([^_]*)_*/${1:/capitalize}/g}()",

推荐阅读