例如:
含有占位符的字符串hello,{name},your birthday is {birthday };
提供的Json对象{name: "czonechan", birthday : "1989-07-02" } ;
替换后为 hello,czonechan,your birthday is 1989-07-02。
实现代码:
复制代码 代码如下:
Object.prototype.jsonToString=function(str) {
o=this;
return str.replace(/\{\w*\}/g, function (w) {
r = w.substr(1,w.length-2);//去除{}
return (o[r]===0)?0:(o[r] ? o[r] : "");//o[r]===0这句是为了实现当值为0时输出0而不是空。
});
};
您可能感兴趣的文章:
- python占位符输入方式实例
- go格式“占位符”输入输出 类似python的input
- 浅谈python中的占位符
- 深入理解结构体中占位符的用法
- javascript 文本框水印/占位符(watermark/placeholder)实现方法
- Python 占位符的使用方法详解