I have quite a few templates that utilize mustache tags to determine where to place user data. These templates are stored as a string.
我有很多模板利用小胡子标签来确定放置用户数据的位置。这些模板存储为字符串。
When I call the render function within mustache, I feed in a complex JSON object that contains a number of arrays of both strings and subdocuments.
当我在胡子中调用渲染函数时,我提供了一个复杂的JSON对象,该对象包含许多字符串和子文档的数组。
I incorrectly declared within the mustache tag to use a particular element within array like so:
我在mustache标记中错误地声明要使用数组中的特定元素,如下所示:
{{dataElementArray[2].subElement}}
{{anotherElement.dataArray[1]}}
Instead I would like to change all occurrences within each template to the proper mustache syntax for addressing elements like so:
相反,我想将每个模板中的所有实例更改为适当的胡须语法,以便寻址元素,如下所示:
{{dataElementArray.2.subElement}}
{{anotherElement.dataArray.1}}
What's the best way to systematically go through each template (represented as a string) and use regex's to change what is in each tag? I have over 50 templates, most of them hundreds of lines long with hundreds of tags within each one.
系统地浏览每个模板(表示为字符串)并使用正则表达式来更改每个标记中的内容的最佳方法是什么?我有超过50个模板,其中大多数是数百行,每个模板中有数百个标签。
I am using JavaScript/Node.js for this app.
我正在为这个应用程序使用JavaScript / Node.js.
2 个解决方案
#1
5
This is a tough (but not impossible) task to do with a single regular expression. Fortunately, there's no reason we have to do it with a single one. A much easier (and more robust) approach is to use two regular expressions: one to match replacement tags (things contained in {{curly brackets}}
) and another to replace instances of array indexers with dot indexers. Here's my solution:
对于单个正则表达式来说,这是一项艰巨的(但并非不可能)的任务。幸运的是,我们没有理由只用一个。一种更容易(也更强大)的方法是使用两个正则表达式:一个匹配替换标记({{curly bracket}}中包含的内容),另一个用点索引器替换数组索引器的实例。这是我的解决方案:
s.replace( /\{\{(.*?)\}\}/g, function(x){ // this grabs replacement tags
return x.replace( /\[(\d+)\]/g,'.$1' )}); // this replaces array indexers
Note: I have not analyzed this solution with the entire mustache syntax, so I cannot guarantee it will work if you're using more than the standard tags.
注意:我没有用整个胡子语法分析这个解决方案,所以如果你使用的不仅仅是标准标签,我不能保证它会起作用。
#2
1
Description
This expression will match the first square bracketed numbers inside the mustache brackets, it also validates the square brackets are followed by either a dot or the close mustache bracket.
此表达式将匹配小胡子括号内的第一个方括号内的数字,它还验证方括号后跟一个点或关闭小胡子括号。
Use this regex: (\{\{[a-z.]*[a-z])\[(\d+)\](?=\.|\})([a-z.]*\}\})
使用此正则表达式:(\ {\ {[a-z。] * [a-z])\ [(\ d +)\](?= \。| \})([a-z。] * \} \})
With this replacement: $1.$2$3
有了这个替代品:$ 1. $ 2 $ 3
Javascript Code Example:
http://www.myregextester.com/?r=f9e362af
Sample Input
{{dataElementArray[2].subElement}}
{{anotherElement.dataArray[1]}}
{{alreadyFormatted.dataArray.1}}
Code
<script type="text/javascript">
var re = /(\{\{[a-z.]*[a-z])\[(\d+)\](?=\.|\})([a-z.]*\}\})/;
var sourcestring = "source string to match with pattern";
var replacementpattern = "$1.$2$3";
var result = sourcestring.replace(re, replacementpattern);
alert("result = " + result);
</script>
After Replacement
{{dataElementArray.2.subElement}}
{{anotherElement.dataArray.1}}
{{alreadyFormatted.dataArray.1}}
#1
5
This is a tough (but not impossible) task to do with a single regular expression. Fortunately, there's no reason we have to do it with a single one. A much easier (and more robust) approach is to use two regular expressions: one to match replacement tags (things contained in {{curly brackets}}
) and another to replace instances of array indexers with dot indexers. Here's my solution:
对于单个正则表达式来说,这是一项艰巨的(但并非不可能)的任务。幸运的是,我们没有理由只用一个。一种更容易(也更强大)的方法是使用两个正则表达式:一个匹配替换标记({{curly bracket}}中包含的内容),另一个用点索引器替换数组索引器的实例。这是我的解决方案:
s.replace( /\{\{(.*?)\}\}/g, function(x){ // this grabs replacement tags
return x.replace( /\[(\d+)\]/g,'.$1' )}); // this replaces array indexers
Note: I have not analyzed this solution with the entire mustache syntax, so I cannot guarantee it will work if you're using more than the standard tags.
注意:我没有用整个胡子语法分析这个解决方案,所以如果你使用的不仅仅是标准标签,我不能保证它会起作用。
#2
1
Description
This expression will match the first square bracketed numbers inside the mustache brackets, it also validates the square brackets are followed by either a dot or the close mustache bracket.
此表达式将匹配小胡子括号内的第一个方括号内的数字,它还验证方括号后跟一个点或关闭小胡子括号。
Use this regex: (\{\{[a-z.]*[a-z])\[(\d+)\](?=\.|\})([a-z.]*\}\})
使用此正则表达式:(\ {\ {[a-z。] * [a-z])\ [(\ d +)\](?= \。| \})([a-z。] * \} \})
With this replacement: $1.$2$3
有了这个替代品:$ 1. $ 2 $ 3
Javascript Code Example:
http://www.myregextester.com/?r=f9e362af
Sample Input
{{dataElementArray[2].subElement}}
{{anotherElement.dataArray[1]}}
{{alreadyFormatted.dataArray.1}}
Code
<script type="text/javascript">
var re = /(\{\{[a-z.]*[a-z])\[(\d+)\](?=\.|\})([a-z.]*\}\})/;
var sourcestring = "source string to match with pattern";
var replacementpattern = "$1.$2$3";
var result = sourcestring.replace(re, replacementpattern);
alert("result = " + result);
</script>
After Replacement
{{dataElementArray.2.subElement}}
{{anotherElement.dataArray.1}}
{{alreadyFormatted.dataArray.1}}