Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  14] [ 3]  / answers: 1 / hits: 18684  / 11 Years ago, tue, march 19, 2013, 12:00:00

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?



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']

More From » regex

 Answers
81

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]; });

[#79490] Monday, March 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aidan

Total Points: 72
Total Questions: 95
Total Answers: 121

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
aidan questions
Mon, Oct 11, 21, 00:00, 3 Years ago
Wed, Sep 29, 21, 00:00, 3 Years ago
Sun, Sep 5, 21, 00:00, 3 Years ago
Thu, Jan 16, 20, 00:00, 4 Years ago
;