Last active
September 30, 2016 07:13
-
-
Save think2011/99ab49b564a56dc1da4c to your computer and use it in GitHub Desktop.
string render
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 替换字符串 ${} | |
* @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