问题来了,比赛一定不能同时匹配(anything)(anything),只能匹配第一组。
这是 jsFiddle,我一直在解决这个问题。
var templates = '<template mini-dash-ytd>'+
'<span class="mini-dash-title">{{title}}</span>'+
'<span class="mini-dash-metric">{{value}}</span>'+
'</template>'+
'<template mini-dash-yesterday>'+
'<span class="mini-dash-title">{{title}}</span>'+
'<span class="mini-dash-metric">{{value}}<span class="mini-dash-performance-{{performance}}"></span></span>'+
'</template>';
function templatitFind(string) {
var regex = new RegExp("<template\\s+"+string+">([^;]*)</template>","ig");
var val = templates.match(regex);
if (val !== null) return val[0];
}
$(function () {
console.log(templatitFind('mini-dash-ytd'));
});