1

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.

Mark
  • 143,421
  • 24
  • 428
  • 436
User123123
  • 485
  • 1
  • 5
  • 13

2 Answers2

1

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 -.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Thanks wors well, when putting it in a string " ${TM_FILENAME_BASE/([A-Z][a-z0-9]*)(?=([A-Z]?))/${1:/downcase}${2:+-}/g}" with a whitepace, I get this Invalid characters in string. Control characters must be escaped.jsonc(262) any ideas? – User123123 Feb 27 '23 at 23:38
  • I'm nor sure what you mean by "when putting it in a string"? It works as is in a snippet. Are you using it in another way? In some cases, various characters that have special meaning in a regex have to be escaped or even double-escaped. – Mark Feb 28 '23 at 02:18
0

Try this variable

${TM_FILENAME/(.)([^A-Z]*)(.)([^.]*)\\.tsx/${1:/downcase}${2}-${3:/downcase}${4}/}
rioV8
  • 24,506
  • 3
  • 32
  • 49