【问题标题】:VSCode snippet, transform lowercase string delimited by underscores into CamelCase?VSCode片段,将下划线分隔的小写字符串转换为CamelCase?
【发布时间】:2019-12-02 18:41:25
【问题描述】:

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

要在 php.json 中编写的自定义 sn-p:(在我遇到困难的地方请参阅“此处需要 REGEX 帮助”)

"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.当提示匹配sn-p时按回车 2. sn-p 提示我 1 美元

所需的输出(在提示输入 $1 时输入“test_input_string”):

protected $test_input_string = null;
public function getTestInputString()
{
    *cursor resolves here (due to $0)*
}

【问题讨论】:

  • 我不确定 Visual Studio 自定义 sn-ps,但在 javascript 中这样的东西可以工作:('formatted_like_this').split('_').map((el)=>el.charAt(0).toUpperCase() + el.slice(1)).join('')。我不确定它是否有帮助,但也许你可以在 Visual Studio 中写一些类似的东西?
  • @Berci 很遗憾,我不能为此使用 JavaScript 字符串函数,但感谢您的及时回复!

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


【解决方案1】:

试试:

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

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

如果没有pascalcase,这就是你可以使用的:

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

【讨论】:

  • 非常感谢马克,这非常有效!非常感谢您的帮助。
  • 出色的第二个选项。这绝对是我对正则表达式阅读的理解。知道 [ this_camel_case => thisCamelCase ] 的正则表达式是什么(第一个单词以 lcase 开头,所有其他单词都以 ucase 开头)吗?
  • 在这里查看我的camelCase答案:stackoverflow.com/questions/48104851/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 2017-02-24
  • 1970-01-01
  • 2010-11-11
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多