javascript 用于胡子式双括号的正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15502629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex for mustache-style double braces?
提问by RandallB
I'm using Mustache-style tags inside of AngularJS. What's the best regex to use to return an array of justthe text inside the mustache braces?
我在 AngularJS 中使用 Mustache 风格的标签。什么是最好的正则表达式,用于返回一个只包含小胡子括号内的文本的数组?
Sample data:
样本数据:
"This could {{be }} a {{ string.with.dots_and_underscores }} of {{ mustache_style}} words which {{could}} be pulled."
Expected output:
预期输出:
['be','string.with.dots_and_underscores','mustache_style','could']
回答by Xophmeister
If you use a global search with .match
, JavaScript won't give the capture groups in its array output. As such, you need to do it twice: Once to find the {{...}}
pairs, then again to extract the names from within them:
如果您使用 全局搜索.match
,JavaScript 将不会在其数组输出中提供捕获组。因此,您需要执行两次:一次找到{{...}}
对,然后再次从其中提取名称:
str.match(/{{\s*[\w\.]+\s*}}/g)
.map(function(x) { return x.match(/[\w\.]+/)[0]; });
回答by Cody
Mod of Douglas Crockford's String.prototype.supplant
道格拉斯·克罗克福德( Douglas Crockford) 的ModString.prototype.supplant
This will interpolate any {param}
you have between handleBars ({}
). I know this answer is somewhat extensive, but I assumed the question was probably regarding interpolation-- either way, I'd advise the reader to study the regex, regardless.
这将插入{param}
您在 handleBars ( {}
)之间的任何内容。我知道这个答案有点广泛,但我认为问题可能与插值有关- 无论哪种方式,我都建议读者研究正则表达式,无论如何。
clear();
function interpolate(str) {
return function interpolate(o) {
return str.replace(/{([^{}]*)}/g, function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
}
}
var terped = interpolate('The {speed} {color} fox jumped over the lazy {mammal}')({
speed: 'quick',
color: 'brown',
mammal: 'dog'
});
console.log(terped);
Hope this helps
希望这可以帮助
回答by woemler
You could try doing this with exec()
instead:
你可以尝试这样做exec()
:
var list = [],
x = '"This could {{be }} a {{ string }} of {{ mustache_style}} words which {{could}} be pulled."',
re = /{{\s*([^}]+)\s*}}/g,
item;
while (item = re.exec(x))
list.push(item[1]);
回答by J0HN
Something like this
像这样的东西
/{{\s?([^}]*)\s?}}/
The values would be in first group (you know, not the 0-group, the 1-group :))
值将在第一组中(您知道,不是 0 组,是 1 组 :))
One more point - this regex is captures everythingbetween {{
and }}
, so all the punctuation marks, braces, dots, etc. If you need only words (possibly separated by underscore or whitespace) this would be more useful for you:
还有一点 - 这个正则表达式捕获了and之间的所有内容,所以所有的标点符号、括号、点等。如果你只需要单词(可能用下划线或空格分隔),这对你更有用:{{
}}
/{{\s?[\w\s]*\s?}}/
回答by Individual11
I really liked the answer @Cody provided, but ran into a scope issue if you wanted to make the object you pass more of a real object and not just a list, so I found an eval trick to change the scope so I thought I would share it.
我真的很喜欢@Cody 提供的答案,但是如果你想让你传递的对象更像一个真实的对象而不仅仅是一个列表,就会遇到一个范围问题,所以我找到了一个 eval 技巧来改变范围,所以我想我会分享它。
function interpolate(str) {
return function interpolate(o) {
return str.replace(/{([^{}]*)}/g, function (a, b) {
let r
with(o){
r = eval(b)
}
return r
});
}
}
var terped = interpolate('The {speed} {fox.color} {mammal[2]} jumped over the lazy {mammal[0]}')({
speed: 'quick',
fox: {
color: 'brown'
},
mammal: ['dog', 'cat', 'fox']
});
console.log(terped)