5

I have this:

${1/([A-Z]*)(?:_)([A-Z]+)*/${1:/downcase}${2:/downcase}/g}

How to make use downcase and capitalize on the same (2) group?

${1/([A-Z]*)(?:_)([A-Z]+)*/${1:/downcase}${2:/downcase/capitalize}/g}

I want to tansform ZXC_ASD to zxcAsd.

Mark
191k32 gold badges555 silver badges577 bronze badges
asked Jul 10, 2018 at 19:43
0

2 Answers 2

6

Try it like this:

"camelCaseSnail": {
"scope": "javascript,typescript",
 "prefix": "log",
 "body": "${1/([A-Z]*)(?:_)(?:([A-Z])([A-Z]+))*/${1:/downcase}${2:/capitalize}${3:/downcase}/g}"
}

enter image description here

Basically, I've changed the second capture group ([A-Z]+)* to a non-capture group that has two inner capture groups (?:([A-Z])([A-Z]+))*, a single letter for camel-case and the rest, which I refer in the replace/transform part: /downcase}${2:/capitalize}${3:/downcase}/

answered Jul 10, 2018 at 21:16
Sign up to request clarification or add additional context in comments.

Comments

2

Apparently coming to vscode v1.58 is a /camelcase modifier. So your case is as easy as

"${1/(.*)/${1:/camelcase}/}"

camelCase transform example

Tested in the Insiders Build. See Add a camelCase transform for Snippet variables. See also https://stackoverflow.com/a/51228186/836330 for another example.


Old answer:

Using the undocumented (see Snippet regex: match arbitrary number of groups and transform to CamelCase) /pascalcase transform, it is quite easy:

"${1/([A-Z]*)(?:_)([A-Z]+)*/${1:/downcase}${2:/pascalcase}/g}"

as the /pascalcase will do both the /capitalize and the /downcase at once.

answered Dec 29, 2019 at 13:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.