0

I know what is happening, but I do not know how to solve it because I am a novice in regular expressions.

The regular expression, takes all the expression, because it takes the initial brackets of {{username and the end of day}}, then it wraps all the code between them.

How should the regular expression be?

var str = "Welcome back {{username}}, how are you ? today is {{day}}",
    regex = /{{.+}}/g,
    matches = str.match(regex);
    
 console.log(matches); // I was waiting for : [{{username}}, {{day}}]
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

2

Use the non-greedy modifier ? after your + quantifier:

var str = "Welcome back {{username}}, how are you ? today is {{day}}",
    regex = /{{.+?}}/g,
    matches = str.match(regex);
    
 console.log(matches); // I was waiting for : [{{username}}, {{day}}]
CRice
  • 29,968
  • 4
  • 57
  • 70