Javascript Regex匹配两个字符串之间的子字符串,但子字符串可以包含DOT(.)



我确实想匹配{{}} 之间的所有子字符串

当此子字符串不包含.时,正则表达式工作正常:

"When he come, {{person}} will give his son {{something}}".match(/{{(w*)}}/g) ; 

结果:

现在,如果此子字符串包含.(点),则regex不会按预期工作:

"When he come, {{person.firstname}} will give his son {{something}}".match(/{{(w*)}}/g) ;

结果:

如何更改正则表达式以使子字符串也包含DOT,例如:person.firstname

或使用此模式匹配{{}} 之间的任何内容

{{([^}]*)}}

解释:

[^}] a character class, anything but "}"

w将仅与a-z, A-Z, 0-9, _匹配。http://www.w3schools.com/jsref/jsref_regexp_wordchar.asp.

使用这个正则表达式也可以包括任意数量的点:

/{{([w.]*)}}/g

相关内容

最新更新