-
-
Save huanglong-zz/47fc0ca1fe648563a810 to your computer and use it in GitHub Desktop.
This file contains 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
String.prototype.format = function() { | |
var formatted = this; | |
var arg = arguments[0]; | |
var regexp = null; | |
var key = null; | |
if(arg === Object(arg) && arg instanceof Array !== true) { | |
for(key in arg) { | |
if(arg.hasOwnProperty(key)) { | |
regexp = new RegExp('\\{{' + key + '\\}}', 'gi'); | |
formatted = formatted.replace(regexp, arg[key]); | |
} | |
} | |
} else { | |
for (var i = 0; i < arguments.length; i++) { | |
var regexp = new RegExp('\\{{' + i + '\\}}', 'gi'); | |
formatted = formatted.replace(regexp, arguments[i]); | |
} | |
} | |
return formatted; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment