JavaScript 模板引擎实现
🌙
手机阅读
本文目录结构
模拟模板引擎的实现
var str = "my name is {0},my age is {1},i can write {2},so ~~";
var ary = ["zhu", "18", "JavaScript"];
//->"my name is zhu,my age is 18,i can write JavaScript,so~~";
str = str.replace(/{(\d)}/g, function () {
// 每一次执行 function
//arguments[0] ->当前大正则捕获的内容 {0}
//arguments[1] ->当前第一个小分组捕获的内容 0
//return value; ->用 value 把大正则捕获的内容替换 {0}被 value 替换了
console.log(arguments);
return ary[arguments[1]];
});
console.log(str);