Search code examples
visual-studio-codereplacepathvscode-snippets

Facing problem in Variable Transformation in VS Code snippets


I'm having the below code of a global-snippet of VS Code

"prints formated path": {
    "prefix": "pp",
    "body": [
        "$BLOCK_COMMENT_START",
        "File Path ->     \"${TM_FILEPATH/\\///g}\"",
        "$BLOCK_COMMENT_END",
    ],
    "description": "prints path"
},

and the output of the above snippet is

"""
File Path ->     "D:\tp\New folder (6)\4. Chapter 4 Packaging\my_package\test.py"
"""

and the expected output is

"""
File Path ->     "D:/tp/New folder (6)/4. Chapter 4 Packaging/my_package/test.py"
"""

Actually I want to replace '\' with '/'

After searching too much on the web I found this page which talks about Variable Transformation in VS Code but it didn't help me. If you know the solution to the problem please suggest


Solution

  • The problem is the parsing of the regex, you have to add a capture group to recognize the backslash and you have to escape the replace slash

      "prints formated path": {
        "prefix": "pp",
        "body": [
            "$BLOCK_COMMENT_START",
            "File Path ->     \"${TM_FILEPATH/(\\\\)/\\//g}\"",
            "$BLOCK_COMMENT_END",
        ],
        "description": "prints path"
      }