1

I have a string:

string = "{{1:123}} asdfadfasdfdfdfsaf {{1212:}}"

This is my current regex pattern:

var pattern = /[{{].*[}}]/;

When use match, I get:

string.match(pattern)
//returns ["{{1:123}} asdfadfasdfdfdfsaf {{1212:}}"]

How do I get just the first bracket chunk, or better yet, get an array that has each bracket chunk? My ideal return result would be:

["{{1:123}}", "{{1212:}}"]
Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
akantoword
  • 2,824
  • 8
  • 26
  • 43
  • 1
    `.*` is greedy, and will gobble up as much of the string as possible. try `.*?` to make it non-greedy. and note that `[{{]` ispointless. that's a character class. it matches a single character, not two. it's literally "if any single one of the characters inside the `[]` are present at this point in the string" – Marc B Jul 15 '16 at 16:49
  • awesome, I adjusted it to /[{][{].*?[}][}]/g; – akantoword Jul 15 '16 at 16:53
  • but why isn't it /[{][{].?[}][}]/g; ? why do you still need the asterisk? – akantoword Jul 15 '16 at 16:54
  • `[{]` is again pointless. a single-char char class is just cargo-cult programming. `[]` is used for specifying mutiple characters, e.g. `[A17bQ#]` or ranges of characters `[a-zA-Z]`. – Marc B Jul 15 '16 at 16:54
  • No need to use `[}]` and `[{]`. The braces are no special regex characters in this case, no escaping is necessary. – Wiktor Stribiżew Jul 15 '16 at 16:54
  • @jlei You need the asterisk to match multiple characters. `?` by itself just means the preceding character is optional. When put after `*` or `+` it means that the quantifier should be non-greedy. – Barmar Jul 15 '16 at 16:55
  • @jlei: no. `.` = "single character". `?` = "zero or one of the previous", `*` = "zero or more of the previous". `.?` matches zero or one characters. `.*` matches zero-or-more. – Marc B Jul 15 '16 at 16:56
  • And `*?` means *match 0 or more, but as few as possible*, and `+?` means *match 1 or more, but as few as possible* – Wiktor Stribiżew Jul 15 '16 at 17:00

1 Answers1

2

You need to remove square brackets and use a lazy quantifier:

var pattern = /{{.*?}}/g;

See demo

The [{{] matches just one { as it is a character class. Same with [}}], it matches 1 }. See more about character classes.

The .* matches as many any chars other than a newline as possible, you get a substring from the first {{ up to the last }} if you use it. Lazy quantifier will match up to the first }}. Also, see What do lazy and greedy mean in the context of regular expressions and especially Lazy Quantifier Solution.

var re = /{{.*?}}/g; 
var str = '{{1:123}} asdfadfasdfdfdfsaf {{1212:}}';
console.log(str.match(re));
 
Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • If you have very long substrings inside `{{` and `}}`, you might want to unroll the lazy dot as `var pattern = /{{[^}]*(?:}(?!})[^}]*)*}}/g;` – Wiktor Stribiżew Jul 15 '16 at 16:57
  • Also, see [*What do lazy and greedy mean in the context of regular expressions?*](http://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions) – Wiktor Stribiżew Jul 15 '16 at 17:00