visual-studio-codevscode-snippets

Perform Multiple transformation in VS Code Snippet (Chaining or Nesting) ath the same time


I'm trying to perform the following Scenario in vs code snippet editor: Variable/Regex/String| format ===> (variable/regex/string|format)/regex/string |format , and here is an example of what i am trying to accomplish:

"${RELATIVE_FILEPATH/.*[\\\\\\/](app[\\\\\\/])(.*?)[\\\\\\/][^\\\\\\/]+?$/$2/}"

for the previous expression if the relative path was as follows:

src\app\dashboard\products\addProduct\page.tsx

running the last snippet will produce the following:

dashboard\products\addProduct

And what i want it to do is to produce the following:

dashboard/products/addProduct

I mean that i want the snippet to give the same result as previous in addition to replacing every backslash with a forward slash. I tried something like the following :

"${RELATIVE_FILEPATH/.*[\\\\\\/](app[\\\\\\/])(.*?)[\\\\\\/][^\\\\\\/]+?$/${2/[\\\\]/\//}/}"

but it doesn't work and produced the following result when running the snippet :

${RELATIVE_FILEPATH/.*[\\/](app[\\/])(.*?)[\\/][^\\/]+?$/${2/[\]///}/}

So , I know the running both transformations in separate lines and using placeholders will give the desired output, but i don't want running the snippet to produce two line of strings


Solution

  • For result

    dashboard/products/addProduct
    

    use following body string

    "${RELATIVE_FILEPATH/(^.*[\\/\\\\]app[\\/\\\\])|([\\/\\\\][^\\/\\\\]+$)|([\\/\\\\])|([^\\/\\\\]+)/${3:+/}$4/g}"