正则表达式胡子式双支撑? [英] Regex for mustache-style double braces?

查看:160
本文介绍了正则表达式胡子式双支撑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AngularJS中使用Mustache样式的标签。什么是最好的正则表达式用于返回只是胡子括号内的文本数组?

I'm using Mustache-style tags inside of AngularJS. What's the best regex to use to return an array of just the text inside the mustache braces?

示例数据:

"This could {{be }} a {{ string.with.dots_and_underscores }} of {{ mustache_style}} words which {{could}} be pulled."

预期产出:

['be','string.with.dots_and_underscores','mustache_style','could']


推荐答案

如果您使用 .match 进行全局搜索,JavaScript将不会在其数组中提供捕获组输出。因此,您需要执行两次:一次找到 {{...}} 对,然后再次从中提取名称:

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:

str.match(/{{\s*[\w\.]+\s*}}/g)
   .map(function(x) { return x.match(/[\w\.]+/)[0]; });

这篇关于正则表达式胡子式双支撑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆