Setting up a snippet in Visual Studio Code with regex

北城余情 提交于 2019-12-01 13:30:56

Let's try it with a character class that takes account of both path separator types and helps us to escape properly at the same time:

{
    "Comment": {
        "prefix": "#",
        "body":  [  
            "<!-- ${TM_FILEPATH/.*[\\/](.*[\\/].*)$$/$1/} -->",
        ]
    },
}

Try something like this:

"Comment": {
    "prefix": "#",
    "body":  [

      "<!-- ${TM_FILEPATH/.*\\\\(.*\\\\.*)$$/$1/} -->",

      "<!-- ${TM_DIRECTORY/.*\\\\(.*)$/$1/}/${TM_FILENAME} -->",
    ]
},

Those two lines in the body should be equivalent. That works for the Windows directory style, like :

 c:\Users\Mark\asdf\experimental\src\js\main.js

Since your path.separators are / try something like:

"<!-- ${TM_FILEPATH/.*\/(.*\.*)$/$1/} -->", 
"<!-- ${TM_FILEPATH/.*\\/(.*\\.*)$/$1/} -->",
"<!-- ${TM_FILEPATH/.*\\\/(.*\\\.*)$/$1/} -->",
"<!-- ${TM_FILEPATH/.*\\\\/(.*\\\\.*)$/$1/} -->",

I just don't know how many backslashes you will need (and I can't test it here) for your OS.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!