質問

I'm writing a node module and I want to find all the partials used by a dust template.

I have

regex = /\{\s*\>\s*("[^"]*").*\}/

and

test = " something { > \"templatename\" randomchars\" key=\"{random}\" } random { > \"base/templatename2\" thing=\"random\" }"

I want to capture both templatename and base/templatename2.

I tried with and without the g flag and I tried regex.exec(test) twice and test.match(regex) but neither of them give me both. What am I doing wrong?

役に立ちましたか?

解決

Make the .* not greedy (.*?) so that it won't cover the following template names :

var matches = test.match(/\{\s*>\s*("[^"]*").*?\}/g)

This gives you the matches, not the submatches. To get the groups, use exec :

var r = /\{\s*>\s*("[^"]*").*?\}/g, m;
while (m = r.exec(test)) {
  console.log(m[1]); 
}

Side notice : there was no need to escape the >.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top