gpt4 book ai didi

regex - VS 代码 : How to convert snippet placeholder from camelCase to SCREAMING_SNAKE_CASE?

转载 作者:行者123 更新时间:2023-12-04 00:53:56 24 4
gpt4 key购买 nike

我想创建一个 VS 代码片段来创建 redux reducer。

我想要一个带有占位符的片段,它需要驼峰式,然后将匹配的占位符转换为 SCREAMING_SNAKE_CASE。

这是我尝试的片段,它不起作用:

"test": {
"prefix": "test",
"body": "${1} -> ${1/([a-zA-Z])(?=[A-Z])/${1:/upcase}_/g}"
},

这会产生非预期的结果:
changeNetworkStatus -> changE_NetworK_Status

所需流量
  • 类型 test(片段名称)
  • 点击标签以加载片段。
  • 键入 changeNetworkStatus 以产生:
    changeNetworkStatus -> changeNetworkStatus
  • 点击标签以获得预期结果:
    changeNetworkStatus -> CHANGE_NETWORK_STATUS

  • 如何更改我的代码段以获得所需的结果?

    Here's a related solution which requires a different flow.

    最佳答案

    更新:键绑定(bind)版本 :
    VScode 正在添加 editor.action.transformToSnakecase在 v1.53 中,因此可以更轻松地完成请求的操作,而无需找出必要的正则表达式来使其工作,如上一个答案所示。并且因为有些人可能会在寻找蛇盒(snake-case)信息时发现这个问题。
    然而,我现在展示的不是片段。您只需键入文本,然后触发键绑定(bind)。键绑定(bind)本身会从 multi-command 触发宏扩展命令。延期。在 keybindings.json :

          {
    "key": "alt+3", // whatever keybinding you wish
    "command": "extension.multiCommand.execute",
    "args": {
    "sequence": [
    "cursorWordLeftSelect", // select word you just typed
    "editor.action.transformToSnakecase",
    "editor.action.transformToUppercase",
    // "cursorLineEnd" // if you want this
    ]
    },
    "when": "editorTextFocus && !editorHasSelection"
    },
    按键绑定(bind)版本演示:
    snake case demo

    片段版 :
    "camelCaseModify": {
    "prefix": "test",
    "body": [

    // first inefficient try, works for up to three words
    // "${1} -> ${1/^([a-z]*)([A-Z])([a-z]+)*([A-Z])*([a-z]+)*/${1:/upcase}_$2${3:/upcase}${4:+_}$4${5:/upcase}/g}"

    "${1} -> ${1/([a-z]*)(([A-Z])+([a-z]+))?/${1:/upcase}${2:+_}$3${4:/upcase}/g}",

    // here is an especially gnarly version to handle edge cases like 'thisISABCTest' and trailing _'s
    "${1} -> ${1/([a-z]+)(?=[A-Z])|([A-Z])(?=[A-Z])|([A-Z][a-z]+)(?=$)|([A-Z][a-z]+)|([a-z]+)(?=$)/${1:/upcase}${1:+_}$2${2:+_}${3:/upcase}${4:/upcase}${4:+_}${5:/upcase}/g}"

    ],
    "description": "underscore separators"
    },
    这适用于任何数量的驼峰式单词,从一到无穷大...... ${2:+_}表示“如果有捕获组 2,则附加下划线。”如果没有第二个单词/捕获组,那么组 3 和 4 无论如何都会为空,因为它们在捕获组 2 中。捕获组 2 始终是下一个单词(以一个大写字母开头,后跟至少一个小写字母)。
    例如,使用 changeNetworkStatus :
    Match 1

    Full match 0-13 `changeNetwork`
    Group 1. 0-6 `change`
    Group 2. 6-13 `Network`
    Group 3. 6-7 `N`
    Group 4. 7-13 `etwork`

    Match 2

    Full match 13-19 `Status`
    Group 1. 13-13 ``
    Group 2. 13-19 `Status`
    Group 3. 13-14 `S`
    Group 4. 14-19 `tatus`

    Match 3

    Full match 19-19 ``
    Group 1. 19-19 ``
    样本输出:
    abcd -> ABCD
    twoFish -> TWO_FISH
    threeFishMore -> THREE_FISH_MORE
    fourFishOneMore -> FOUR_FISH_ONE_MORE
    fiveFishTwoMoreFish -> FIVE_FISH_TWO_MORE_FISH
    sixFishEelsSnakesDogsCatsMiceRatsClocksRocks -> SIX_FISH_EELS_SNAKES_DOGS_CATS_MICE_RATS_CLOCKS_ROCKS
    使用 regex101.com真的有助于想象正在发生的事情!

    关于regex - VS 代码 : How to convert snippet placeholder from camelCase to SCREAMING_SNAKE_CASE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51277998/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com