首页 > 解决方案 > Visual Studio Code Snippet Variable Transform not working

问题描述

I'm trying to make a snippet that inserts the last two directorys of the current filepath.

My code:

${TM_DIRECTORY/\\(.*)\\([a-zA-Z]+)\\([a-zA-Z]+)/$1\\$2/}

So when Filepath is
"...\htdocs\projectname\src"
the output should be
"projectname\src".
But instead I get this result:
${TM_DIRECTORY/(.*)\\([a-zA-Z]+)\\([a-zA-Z]+)/$1/}

What am I doing wrong?

标签: variablesvisual-studio-codetransformcode-snippets

解决方案


问题

问题是代码转换 \\\. 例如,如果你想写\w,那么你必须写\\w在片段中。

同样的方式.. 你必须写\\\\在片段 json 中,这样它才能转换成//.

解决方案

  1. ${TM_DIRECTORY/.*?\\\\([a-zA-Z]+\\\\[a-zA-Z]+)$/$1/}

或者,我认为您应该使用\w,而不是[a-zA-Z]因为目录名称可以包含一些字符,例如-or_等​​。

  1. ${TM_DIRECTORY/.*?\\\\(\\w+\\\\\\w+)$/$1/}

推荐阅读