2

I’m trying to create a regex for a custom VS Code snippet I’m writing. Im taking the file name that’s in camel case format and modifying it so a hyphen separates each word (before the uppercase letter) and then transforms the entire thing to lowercase. For example, if my file name was titled myFileName, the result would be my-file-name. Appreciate the help in advance!

I’m able to create the expression for hyphenating between words with ([a-z])([A-Z])/$1-$2/ but I’m not positive the if that’s the best way to do that. After that expression I’m not able to then transform to lower case

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Kate
  • 21
  • 2

1 Answers1

1

In Visual Code snippets, the /downcase operator must be used to lowercase a capturing group value:

([a-z])([A-Z])/$1-${2:/downcase}/

See the EBNF (extended Backus-Naur form) for snippets.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks so much that worked and is exactly what I was looking for. Didn’t realize this but some file names have periods in them. Do you know how to accomplish this but removing the periods (or replacing with hyphen)? Thanks! – Kate Apr 09 '23 at 22:02
  • @Kate It is not that simple. You cannot use backreferences inside conditional replacement pattern, so I do not think it is possible here. – Wiktor Stribiżew Apr 09 '23 at 22:24