I'd like to create a VS Code snippet for creating redux reducers.
I would like to have a snippet with placeholder that expects camelCase and then transform a matching placeholder to SCREAMING_SNAKE_CASE.
Here's my attempted snippet, which is not working:
"test": {
"prefix": "test",
"body": "${1} -> ${1/([a-zA-Z])(?=[A-Z])/${1:/upcase}_/g}"
},
Which produces a non-desired result:
changeNetworkStatus -> changE_NetworK_Status
Desired Flow
- type
test
(name of snippet) - hit tab to load the snippet.
type
changeNetworkStatus
to result in:changeNetworkStatus -> changeNetworkStatus
hit tab to get expected result of:
changeNetworkStatus -> CHANGE_NETWORK_STATUS
How can I change my snippet code to get the desired result?
This works with any number of camelCase words, from one to infinity...
The
${2:+_}
means "if there is a capture group 2 then append an underscore." If there isn't a second word/capture group then groups 3 and 4 will be empty anyway because they are within capture group 2. Capture Group 2 is always the next Word (that starts with one capital and followed by at least one small letter).for example, using
changeNetworkStatus
:Sample Output:
Using regex101.com really helps to visualize what is going on!