0

I have this template (mustache alike):

{{varone}}

{{vartwo}}

{{#varthree}}
  {{subvarone}}
  {{subvartwo}}
{{/varthree}}

{{#varfour}}
  {{subvarthree}}
  {{subvarfour}}
{{/varfour}}

{{^varfive}}
  some message
{{/varfive}}

{{age}}

{{var_a}} {{#var_b}} {{var_c}} {{/var_b}}

and I want to get varone, vartwo, varthree, varfour, varfive and varsix, but not subvars inside blocks. I have a regexp to get subgroups but it is not working well, and I tried to get every expression that does not have dash, but it algo gets subvars...

UPDATE: also it should work in single line, so it should get var_a, var_b but not var_c...

javascript: //template has the template descibed above. console.log("TEMPLATE >> ",template);

matches = template.match(/{{\s*\#\w+\s*}}[^#]*{{\s*\/\w+\s*}}/g) || [];
console.log("MATCHES groups >> ", matches);

matches = template.match(/{{\s*[\w\.\^]+\s*}}/g) || [];
console.log("MATCHES all >> ", matches);

please note that in javascript we need a trick to make dot matches also breaklines, by having [\s\S], in this case I decided to include everything but the dash to collect subexpressions. This is the console result:

MATCHES groups >>  [ '{{#varthree}}\n  {{subvarone}}\n  {{subvartwo}}\n{{/varthree}}',
  '{{#varfour}}\n  {{subvarthree}}\n  {{subvarfour}}\n{{/varfour}}\n\n{{^varfive}}\n  some message\n{{/varfive}}' ]
MATCHES all >>  [ '{{varone}}',
  '{{vartwo}}',
  '{{subvarone}}',
  '{{subvartwo}}',
  '{{subvarthree}}',
  '{{subvarfour}}',
  '{{^varfive}}',
  '{{age}}' ]
alexserver
  • 1,348
  • 3
  • 15
  • 30
  • 1
    possible duplicate of [How do you access the matched groups in a JavaScript regular expression?](http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression) – Meredith Aug 05 '14 at 20:41

1 Answers1

1

I want to get varone, vartwo, varthree, varfour, varfive and varsix, but not subvars inside blocks.

Get the matched group from index 1.

(?:\n|^){{([^}]*)}}

Here is DEMO

Pattern explanation:

  (?:                      group, but do not capture:
    \n                       '\n' (newline)
   |                        OR
    ^                        the beginning of the string
  )                        end of grouping
  {{                       '{{'
  (                        group and capture to \1:
    [^}]*                    any character except: '}' (0 or more times)
  )                        end of \1
  }}                       '}}'

You can try Lazy pattern as well.

(?:\n|^){{(.*?)}}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • If it doesn't work then try `(?:\r?\n|^){{([^}]*)}}` – Braj Aug 05 '14 at 20:58
  • Replace `\r?\n` with `\R`. I covered the technique [here](http://stackoverflow.com/a/25091724/3622940). – Unihedron Aug 06 '14 at 00:01
  • @user3218114 with this pattern I was getting also the closing tags, so I modified to this: ```/(?:\n|^){{[\w\#\^]([^}]*)}}/gmi``` and now it works pretty well. Thanks – alexserver Aug 06 '14 at 14:50
  • @user3218114 what if I would like to have those templates in a single line ? kind of ```{{varone}} {{#vartwo}} {{subvarone}} {{/vartwo}}``` .Same requirement, just get the varone and vartwo. – alexserver Aug 06 '14 at 17:12
  • For single line just add space in the regex `(?:\n|^| ){{([^}]*)}}` – Braj Aug 06 '14 at 19:33
  • @user3218114 isn't space captured as \s in javascript regex ? – alexserver Aug 07 '14 at 18:32
  • The `\s` metacharacter is used to find a `whitespace` character. [Read more](http://www.w3schools.com/jsref/jsref_regexp_whitespace.asp) and it includes `space`, `tab`, `carriage return`, `new line`, `vertical tab` and `form feed` – Braj Aug 07 '14 at 18:33