Sublime Forum

[Solved] Snippet Issue, issue with multiple replacements

#1

Hi There!

I’m trying to generate this output using a snippet:

replacementone
replacementtwo

Replacement:
AAReplacementoneBBBReplacementtwoDDDD

Standalone:
Replacementtwo

Using the following snippet:

${1:replacementone}
${2:replacementtwo}

Replacement:
AA${1/(\w+)/\u$1/g}BBB${2/(\w+)/\u$2/g}DDDD

Standalone:
${2/(\w+)/\u$2/g}

However, when I run it I only get the first replacement to work…

replacementone
replacementtwo

Replacement:
AAReplacementoneBBBDDDD

Standalone:

The field for replacementone works but the other one goes unrecognized…
Any help would be greatly appreciated!

Thanks!

Edit:

If I use only use the replacement of the first field it works as intended… I.e. If the snippet is

${1:replacementone}
${2:replacementtwo}

Replacement:
 AA${1/(\w+)/\u$1/g}BBB${1/(\w+)/\u$1/g}DDDD

Standalone:
${1/(\w+)/\u$1/g}

Then the output is

replacementone
replacementtwo

Replacement:
AAReplacementoneBBBReplacementoneDDDD

Standalone:
Replacementone

Edit 2:
I’ve tried changing the flags, with i, ig, g, and none but no avail, this is driving me insane.

0 Likes

#2

There is no 2nd capture group in your regex (\w+), hence $2 is an empty string. That \u is mysterious to me too. ${2/(\w+)/$1/g} should work.

2 Likes

#3

Thank you very much! This was indeed the right answer ${2/(\w+)/\u$1/g}

The \u character transforms the first character of the capture group into an uppercase

2 Likes