vscode 片段 - 转换和替换文件名

Han*_*Che 2 regex visual-studio-code vscode-snippets

我的文件名是

some-fancy-ui.component.html

我想使用 vscode 片段将其转换为

SOME_FANCY_UI

所以基本上

  1. 对每个字符应用大写
  2. 全部替换 - 用 _
  3. 删除 .component.html

目前我有

'${TM_FILENAME/(. )(-)(. )/${1:/upcase}${2:/_}${3:/upcase}/g}'

这给了我这个

'设置-打印机-服务器-LIST.COMPONENT.HTML'

文档没有解释如何将替换与其对正则表达式组的转换结合使用。

Wik*_*żew 7

如果您需要上层的块被分隔,-或者.您可以使用

"Filename to UPPER_SNAKE_CASE": {
    "prefix": "usc_",
    "body": [
        "${TM_FILENAME/\\.component\\.html$|(^|[-.])([^-.]+)/${1:+_}${2:/upcase}/g}"
    ],
    "description": "Convert filename to UPPER_SNAKE_CASE dropping .component.html at the end"
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处检查正则表达式的工作情况

  • \.component\.html$-.component.html在字符串末尾匹配
  • | - 或者
  • (^|[-.])将字符串或-/ 的开头捕获.到组 1
  • ([^-.]+)捕捉以外的任何字符1+-.成2组。

${1:+_}${2:/upcase}置换方式:

  • ${1:+ - 如果第 1 组不为空,
  • _ - 用。。。来代替 _
  • } - 第一组处理结束
  • ${2:/upcase} - 将较高的第 2 组值放回原处。