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

时间:2019-06-27 14:17:24

标签: 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}'

这给了我

  

“ SETUP-PRINTER-SERVER-LIST.COMPONENT.HTML”

文档没有说明如何将替换及其转换应用于正则表达式组。

1 个答案:

答案 0 :(得分:1)

如果需要将上面的块用-.隔开,则可以使用

"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"
}

您可以选中regex workings here

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

${1:+_}${2:/upcase}的替换含义是:

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