skip to Main Content

I am building my own snippet in vscode.
I am not the best with regex so I was using chatgpt to help me build regex.
However, I am unable to create a regex that will convert my file name such as:

AppInput.tsx into app-input

Currently, I have this expression TM_FILENAME_BASE/[^0-9a-z]+//gi for converting my file name:

AppInput.tsx into AppInput

Looking for a solution.

2

Answers


  1. Try this variable

    ${TM_FILENAME/(.)([^A-Z]*)(.)([^.]*)\.tsx/${1:/downcase}${2}-${3:/downcase}${4}/}
    
    Login or Signup to reply.
  2. This works on file names like App.tsx, AppInput.tsx., AppInputMore.tsx, etc. for any number of groups in the filename:

    "modify FileName":{
      "scope": "typescriptreact",   // if you want to restrict to javascriptrect files
      "prefix": "case",             // whatever you want for a prefix
      "body": [
        "${TM_FILENAME_BASE/([A-Z][a-z0-9]*)(?=([A-Z]?))/${1:/downcase}${2:+-}/g}",
      ]
    }
    

    ${2:+-} is a conditional, only if there is a capture group 2 add the following -.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search