当前位置: 首页 > 后端技术 > Node.js

8行代码模板字符串替换函数

时间:2023-04-03 14:28:44 Node.js

特点无依赖无检查无错误处理无逻辑无配置代码functionrender(tpl,data){varre=/{{([^}]+)?}}/;变量匹配='';while(match=re.exec(tpl)){tpl=tpl.replace(match[0],data[match[1]]);}returntpl;}demovartpl='/cube_xinbao_dial_result/{{action}}/{{report_type}}/{{query}}/?userId={{userId}}';vardata={report_type:1,query:'2323',action:'todolist',userId:'23234234'}functionrender(tpl,data){varre=/{{([^}]+)?}}/;变量匹配='';while(match=re.exec(tpl)){tpl=tpl.replace(match[0],data[match[1]]);}returntpl;}console.log(render(tpl,data));>/cube_xinbao_dial_result/todolist/1/2323/?userId=23234234

猜你喜欢