Json对象替换字符串占位符实现代码

时间:2010-11-17    点击:429   
例如:

  含有占位符的字符串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而不是空。
});
};
菜鸟学习JavaScript小实验之函数引用
return false,对阻止事件默认动作的一些测试代码
javascript利用初始化数据装配模版的实现代码
javascript拓展DOM操作 prependChild insertAfert
Javascript 静态页面实现随机显示广告的办法
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved