function formatString(str, ...args) {
    return str.replace(/{(\d+)}/g, function(match, index) { 
      return typeof args[index] != 'undefined'
        ? args[index]
        : match
      ;
    });
}

var str = "Hello, my name is {0} and I am {1} years old.";
var result = formatString(str, "John", 30);

这将创建一个字符串:“Hello, my name is John and I am 30 years old.”

 

 posted on 2023-07-28 17:14  絆τ  阅读(29)  评论(0编辑  收藏  举报