Home > OS >  Replace and upper case in a snippet with vscode
Replace and upper case in a snippet with vscode

Time:10-13

I'd like to replace Context in myAppContext and upper case on tab pressing, so the end result should be MyApp.

I would rather do it all at once. Here's where I started.:

To remove 'Context' I can do ${1/Context//} and for uppercase: ${1/(^[a-z])/${1:/upcase}/}.

What is the best way to combine these two?

CodePudding user response:

You can use

${1/^([a-z])|Context/${1:/upcase}/g}

See the regex demo.

Here, ^([a-z])|Context matches either a lowercase ASCII letter at the start of the string (capturing it into Group 1) or a Context substring, and replaces the match with the uppercased Group 1 value (which will be empty if Context matches.

CodePudding user response:

Using the capitalize transform this is very easy:

${1/(.*)Context/${1:/capitalize}/}

Just capitalize everything before Context - capitalize only works on the first letter anyway. No need for a g flag.

  • Related