VSCode 片段:将 PascalCase 转换为带空格的小写

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

我正在尝试编写一个将

PascalCase
转换为
lowercase with spaces
的 vscode 片段。我快到了:

${1/([A-Z]*)([A-Z][a-z]+)/$1 ${2:/downcase} /g}

第 1 组匹配首字母缩略词,第 2 组匹配大写单词。

因此,如果占位符是

MyUIDTest
,则预期结果将是
my UID test
。但目前我得到了
 my UID test 
(注意两边的空格)。

如果第 1 组有匹配项,如何仅在第 1 组之后添加空格?如何删除行尾的空格?

regex regex-negation vscode-snippets
1个回答
0
投票

根据文档

${1:+ }
表示“插入一个空格iff第1组被捕获”:

${
  TM_SELECTED_TEXT
  /
    (?<=([A-Za-z])?)      # Lookbehind for and capture ($1) a preceding letter, if any,
    (                     # then match and capture ($2) a "word" – either
      [A-Z]+(?![a-z])     # 1+ uppercase not followed by a lowercase
    |                     # or
      [A-Z][a-z]+         # an uppercase followed by 1+ lowercase.
    )                     # 
  /                       # Replace with
    ${1:+ }               # a space iff group 1 is captured
    ${2:/downcase}        # then the lowercase version of group 2
  /g                      # for all matches found.
}

我在这里使用

TM_SELECTED_TEXT
,但您可能想根据您的需要进行更改。

© www.soinside.com 2019 - 2024. All rights reserved.