How do I access a group in a regex of a show rule?

I want to build a show rule that uses a regular expression to find text passages that are enclosed in curly brackets. In the output, the curly brackets should be replaced by square brackets. And the entire expression should be displayed in red.

To achieve this, I need to get access to the text between the curly brackets. That would be the first group in the regular expression. However, the it binding includes the complete expression. That’s why my show rule returns ‘Hello [{World}]!’ instead of ‘Hello [World]!’.

How do I get the group?

#show regex("\{(.*?)\}"): it => {
  set text(fill: red)

  [\[#it\]]
}

Hello {World}!

it is already the matching text as content, the other fields that .match() would return are therefore not accessible in the show rule.

If you want to replace the curly brackets by square brackets, you could use two show rules inside your regex show rule. If you want to completely remove the curly brackets, just use empty strings as the replacements.

#show regex("\{(.*?)\}"): it => {
  show "{": "["
  show "}": "]"
  set text(red)
  it
}

If there is the possibility that you could have single curly brackets in your document, you can use the following pattern instead to always match the innermost pair of curly brackets.

regex("\{[^\{\}]*?\}")