简易模板引擎
1 function t(s, data){ 2 var r = [], i = 0, len = data.length; 3 for(; i < len; i++){ 4 r.push(s.replace(/\$[\w\d]+/g, function(m){ 5 return (m = m.substr(1)) in data[i] ? data[i][m] : ''; 6 })) 7 } 8 return r.join('\r\n'); 9 } 10 11 console.log(t('<li>$name<span>$age</span></li>', [ 12 { name : 'qj', age : 27 }, 13 { name : '11111', age : 127 }, 14 { name : '222', age : 3 }, 15 ]));