String. Format = function (str ){
Var reg;
Var args;
// Customize an undifined to increase the speed
Var undifined;
If (arguments [1] & typeof (arguments [1]) = "object "){
Reg = new RegExp ("\\{ ([^ \ d] \\ w *) \\}", "g ");
Args = arguments [1];
}
Else {
Reg = new RegExp ("\\{ (\ d +) \\}", "g ");
// Convert arguments into an array and assign it to args
Args = Array. prototype. slice. call (arguments );
For (var I = 0; I <args. length-1; I ++)
{
Args [I] = args [I + 1];
}
// Remove the first value of args because the first value is a string that needs to be matched by regular expressions.
Args [I] = undifined;
}
Return str. replace (reg, function (){
Var val = args [arguments [1];
Return val = undifined? Arguments [0]: val;
});
};
Alert (String. Format ("{0}, {1}, {2}", "aa", "bb "))
Alert (String. Format ("{aa}, {bb}", {aa: 123, bb: 456 }))
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service