Search code examples
variablesvisual-studio-codetransformcode-snippets

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?


Solution

  • Problem:

    The issue is the code converts \\ to \. For example if you want to write \w, then you have to write \\win snippet.

    The same way.. You have to write \\\\ in snippet json, so that it shall convert into //.

    Solution:

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

    or, I think you should use \w instead of [a-zA-Z] because the directory name can contain some characters like - or _ etc.

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