Skip to content

Instantly share code, notes, and snippets.

@think2011
Last active September 30, 2016 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think2011/99ab49b564a56dc1da4c to your computer and use it in GitHub Desktop.
Save think2011/99ab49b564a56dc1da4c to your computer and use it in GitHub Desktop.
string render
/**
* 替换字符串 ${}
* @param obj
* @returns {String}
* @example
* '我是${str}'.render({str: '测试'});
*/
String.prototype.render = function (obj) {
var str = this, reg;
Object.keys(obj).forEach(function (v) {
reg = new RegExp('\\$\\{' + v + '\\}', 'g');
str = str.replace(reg, obj[v]);
});
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment