You need to enable JavaScript to run this app. 如何用正则配置VS Code snippet实现大驼峰及蛇形命名转换
优惠活动
大模型
产品
解决方案
定价
更多

如何用正则配置VS Code snippet实现大驼峰及蛇形命名转换

VS Code Snippet 正则实现方案

以下是两个需求的完整实现代码:

需求1:kebab-case 字符串转大驼峰(PascalCase)

可直接在 VS Code 全局 snippets 配置中添加如下配置:

"KebabCase to PascalCase": {
  "prefix": "kebab-pascal",
  "body": "${1/(-?)(\\w)/${2:/upcase}/g}",
  "description": "Convert kebab-case string to PascalCase format"
}

实现逻辑:正则(-?)(\w)会匹配所有带可选横杠前缀的单词字符,替换时丢弃横杠,仅将匹配到的单词字符转为大写,输入some-component即可输出SomeComponent。

需求2:小驼峰字符串生成指定createMessage代码

对应snippet配置如下:

"CamelCase to createMessage": {
  "prefix": "camel-msg",
  "body": "const ${1:funcName} = createMessage(${1/([a-z])([A-Z])/${1}_${2:/upcase}/g;/upcase});",
  "description": "Generate createMessage code from camelCase input"
}

实现逻辑:首先用正则([a-z])([A-Z])匹配小驼峰中「小写字母+大写字母」的组合,替换为「小写字母_大写字母」格式,再将整体转为全大写,输入sendData即可输出const sendData = createMessage(SEND_DATA);。


内容的提问来源于stack exchange,提问作者Lucas Gomes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 00:51:02