0

I have an regex to replace some input, this is the input {{number}}-{{$index}}

And i want transform to result = ["{{number}}", "{{$index}}"]

I trying with this regex ({{).*?(}}) but don't give me what i'm expect

jannowitz
  • 84
  • 7
  • What language are you using? What is the replacement string you're using? – Barmar Feb 27 '18 at 18:07
  • Are `number` and `$index` literal strings, or are they variable? – Barmar Feb 27 '18 at 18:07
  • I'm using javascript, they are variables –  Feb 27 '18 at 18:08
  • Add the language tag and post your code. – Barmar Feb 27 '18 at 18:08
  • 3
    From the way this is worded, it looks like you could do `result = input.split("-")` – Dan Prince Feb 27 '18 at 18:11
  • I'm just try to using a regex to modify the variable content, they come with this symbols, i'm sorry they are not variables, they are a literal string and i want apply this regex to transform –  Feb 27 '18 at 18:12
  • 1
    @JoãoVictorCanabarro can you **explain your intent**? It's not clear what you're trying to accomplish. Take a step back and read your question as if you have no preconceived knowledge about the task at hand. – ctwheels Feb 27 '18 at 18:15
  • The string `{{number}}-{{$index}}` comes for a outside function, but between `{{number}}` and ` {{$index}}` could has anything and i want remove this and just take this 2 parts of the text, just only that –  Feb 27 '18 at 18:19
  • Just match on [`{{[^{}]*}}`](https://regex101.com/r/nxl5GC/1) and if you want to match single `{` or `}` inside those brackets use [`{{(?:(?!{{|}}).)*}}`](https://regex101.com/r/nxl5GC/2) – ctwheels Feb 27 '18 at 18:20
  • Does one of the variables contain { or }? – StyleSh1t Feb 27 '18 at 18:23
  • @ctwheels Could you please add this to the answer of the post –  Feb 27 '18 at 18:24

1 Answers1

0

To extract all {{x}} where x can be any character except { or } you can use the following:

See regex in use here

{{[^{}]*}}
  • {{ Match this literally
  • [^{}]* Match any character except { or } any number of times
  • }} Match this literally

Similarly, the following regex does the same as the previous but allows single { and }, but not double {{ or }}:

See regex in use here

{{(?:(?!{{|}}).)*}}
  • {{ Match this literally
  • (?:(?!{{|}}).)* Match any character any number of times except where {{ or }} exists. This is called a tempered greedy token.
  • }} Match this literally

For a very slight performance gain, you can substitute each instance of {{ for {{2} and each instance of }} for }{2}.

ctwheels
  • 21,901
  • 9
  • 42
  • 77