记录一个生僻知识点 —— JS字符模板替换
好!今天我们来记录生僻知识点!
首先我先放一段代码在下面:
let str = "${name} in the ${area} eat ${food}!" let o = { name : "吴亦凡", area: "监狱", food:"大碗牢饭"} function render(str,obj){ let keys = []; for(let i in obj){ keys.push(i) } keys.map((e)=>{ let reg = new RegExp("\\${"+e+"}","g"); str = str.replace(reg, obj[e]) }) return str; } console.log(render(str,o))
至此结束!