visual-studio-codecode-snippetsvscode-snippets

VS Code snippet - how to get the relative_filepath without the file extension


How can I get the relative_filepath without the extension and backslash change ?

I found two transforms:

  1. "${RELATIVE_FILEPATH/[\\]/\//g/}"
  2. "${RELATIVE_FILEPATH/(.js)//}"

If the relative file path is \aaa\bbb\ccc.js

I tried this:

/${RELATIVE_FILEPATH/((.js))}|/[\\]/${1://}${2://g/}/}

I want to get text like this: /aaa/bbb/ccc

Thank you.


Solution

  • Try this snippet:

    "Remove extension and swap to forward slash": {
        "prefix": "t2",
        "body": [
            // "${RELATIVE_FILEPATH}",
            "/${RELATIVE_FILEPATH/.js|(\\\\)/${1:+\/}/g}",
        ],
        "description": ""
    },
    

    Note the double escapes in .js|(\\\\) are necessary.
    And I added the + to the ${1:+\/}.