0

I'm currently using the below regex:

/{{([^}]+)}}/g

This does well at matching all text in a string that are wrapped in double curly brackets, but there's a problem when the text is wrapped in triple curly brackets.

In this case, it matches the first three curly brackets, but not the last one.

I'd essentially like a regex that matches text only wrapped in double curly brackets.

So the bold text below would need to be matched.

{{{Text}}}

Image for regex101 test using my current regex

jonnoMK
  • 27
  • 5
  • 1
    **tl;dr** You can't, because the JS regex engine doesn't support recursive regexes. – ndnenkov Aug 30 '22 at 15:11
  • You want to match `{{some word}}}}` as `some word}}`? – smac89 Aug 30 '22 at 15:21
  • @ndnenkov: If the brackets are always next to each other, like in example, its just a character like any other. – Poul Bak Aug 30 '22 at 15:36
  • @PoulBak but you can't guarantee that you'll be matching the same number of brackets on both sides. – ndnenkov Aug 30 '22 at 17:27
  • @ndnenkov: Sure you can, like one match for 1 char, then OR for 2 chars etc. – Poul Bak Aug 30 '22 at 17:32
  • @PoulBak and when does it end? What if I have 102301 on both sides? – ndnenkov Aug 30 '22 at 18:28
  • In my example above, I'd like it to match {{three}} from the string, ignoring the extra outer curly brackets. Currently, you can see it's matching {{{three}} and ignoring only the last curly bracket. I just need to extract all words wrapped in exactly two curly brackets. – jonnoMK Aug 31 '22 at 09:05
  • A small change makes it work: `/{{([^{}]+)}}/g`. – Poul Bak Sep 04 '22 at 02:17

2 Answers2

1

You may use this regex:

{{[^{}]*}}

RegEx Breakup:

  • {{: Match {{
  • [^{}]*: Match 0 or more of any char that is not { and }
  • }}: Match }}

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

If your goal is to match anything surrounded by just double brackets, this should work:

/(?:{{(.*)}})/g

See example here

If you don't care about the brackets at all, then this should work: You can use quantifiers to greedily match the brackets:

/{+([^}]+)}+/g

If your goal is to match the same number of brackets on either side of the word, then see these answers here.

smac89
  • 39,374
  • 15
  • 132
  • 179
  • With a lookahead like `/{{(.*?)}}(?=[^}]|$)/g` it would also match `{{{three}}}` as `{three}`. As it ignores amount of closing brackets and the capture just finishes before the last two. So `{{bar}}}}` will be captured as `bar}}`. – Christopher Aug 31 '22 at 08:18
  • Thanks for your response. As per my additional comment in my original question, in my example, I'd like it to match {{three}} from the string, ignoring the extra outer curly brackets. Currently, you can see it's matching {{{three}} and ignoring only the last curly bracket. I just need to extract all words wrapped in exactly two curly brackets – jonnoMK Aug 31 '22 at 09:07
  • @jonnoMK regex is not the best tool for doing this. The RE i mentioned will work with your example, but may fail in others like `{{b{{a}}r}}` matching it as `b{{a`. – Christopher Aug 31 '22 at 09:46