我有一个模版(胡须一样):
{{varone}}
{{vartwo}}
{{#varthree}}
{{subvarone}}
{{subvartwo}}
{{/varthree}}
{{#varfour}}
{{subvarthree}}
{{subvarfour}}
{{/varfour}}
{{^varfive}}
some message
{{/varfive}}
{{age}}
{{var_a}} {{#var_b}} {{var_c}} {{/var_b}}我想让varone,vartwo,var3,var-4,var5和var6,但不是子块。我有一个regexp来得到子组,但是它不能很好地工作,我试着得到每个没有破折号的表达式,但是它总是得到子类.
更新:而且它应该在单行中工作,所以它应该得到var_a,var_b,而不是var_c.
javascript: //template在上面删除了模板。Console.log(“模板>> ",模板);
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);请注意,在javascript中,我们需要一个技巧来使点匹配(也是断线),因为有\s\S,在本例中,我决定包括除了收集子表达式的破折号之外的所有内容。这是控制台的结果:
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}}' ]发布于 2014-08-05 20:38:43
我想要瓦隆,静脉曲张和静脉曲张,但不是块内的子午线。
从索引1中获取匹配的组。
(?:\n|^){{([^}]*)}}这是演示
模式解释:
(?: 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
}} '}}'你也可以试试懒散的模式。
(?:\n|^){{(.*?)}}https://stackoverflow.com/questions/25147765
复制相似问题