var format = function(str, obj){ var data = obj; if(typeof data === 'object'){ return str.replace(/\{([\d\w]+)\}/g, function (m, n) { //替换掉 {xxx} 格式的字符串 return (typeof data[n] !== 'undefined') ? data[n].toString() : m; }); } }, dom = '<div>{name},我的博客地址:{link}</div>', json = { name: '大马德', link: 'http://home.cnblogs.com/u/damade/' } format(dom,json) //输出 <div>大马德,我的博客地址:http://home.cnblogs.com/u/damade/</div>