Search code examples
visual-studio-codevscode-snippets

Compose couple of regular expression in vscode snippets


I trying to build build vscode snippets based on file name. For example: from file first.actions.ts I'm generating First = 42; via const ${TM_FILENAME_BASE/(.*?).actions/${1:/capitalize}/g} = 42;.

However in case when file name is e.g. first-module.actions.ts I'm generating First-Module = 42; and this code contains syntax error. I want const FirstModule = 42;.

I'm looking for some trick to merge regular expression for cutting .actions and remove all - marks.


Solution

  • Try:

    "const ${TM_FILENAME_BASE/([^-]*)-?(.*)\\.actions/${1:/capitalize}${2:/capitalize}/g} = 42;"

    1. capture everything up to the first -,

    2. ignore an optional -, and

    3. capture anything after that up to .actions

    Assuming you want first-module.actions.ts => FirstModule

    Note your extension separator . should be double-escaped to work properly.