-1

I have strings like {{foo}}, {{bar}, {{foo:xyz}}, where inside the curly braces a variable is defined and it can have an optional named modifier when separated with a colon. I want to extract these two parts into capture groups.

  • {{foo}} should give me foo as variable
  • {{bar}} should give me bar as variable
  • {{foo:xyz}} should give me foo as variable and xyz as modifier

Right now I have the following exemplary regex which matches on the foo variable (the foo part is injected dynamically for all possible variables).

/\{\{(foo):?(.*?)\}\}/

This works as expected, but if I match on {{foobar}}, instead of having no match, I get a match with foo as variable and bar as modifier.

How can I alter the regex, so that the second group is only captured if a colon is before it?

0 Answers0