Search code examples
regexvisual-studio-codecode-snippetsvscode-snippets

Replace and upper case in a snippet with vscode


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?


Solution

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