[最佳化]JavaScript 格式化帶有預留位置字串

來源:互聯網
上載者:User
/** * Format a message with placeholder. * * Examples:  * format("X{0}Y{1}Z{2}") : XYZ * format("X{0}Y{1}Z{2}", '1') : X1YZ * format("X{0}Y{1}Z{2}", '1', '2') : X1Y2Z * format("X{0}Y{1}Z{2}", '1', '2', '3') : X1Y2Z3 * format("X{0}Y{1}Z{2}", '1', '2', '3', '4') : X1Y2Z3 * ------------------------------------ * format() : null * format("X{0}Y{1}Z{2}", null) : XYZ * format(null, '1') : null * ------------------------------------ * format("{0{0}1{1}2{2}}") : {012} * format("{0{0}1{1}2{2}}", 'x') : {0x12} * format("{0{0}1{1}2{2}}", 'x', 'y') : {0x1y2} * format("{0{0}1{1}2{2}}", 'x', 'y', 'z') : {0x1y2z} *  * @Author http://blog.csdn.net/xxd851116 */function format( message ) {if (!message) return null;var ss = message.split(/\{\d+?\}/);for ( var i = 0; i < ss.length; i++ ) {if (!arguments[i + 1]) break;ss[i] += arguments[i + 1];}return ss.join("");}

測試案例:

document.writeln("format(\"X{0}Y{1}Z{2}\") : "+ format("X{0}Y{1}Z{2}") + "<br/>");document.writeln("format(\"X{0}Y{1}Z{2}\", '1') : "+ format("X{0}Y{1}Z{2}", '1') + "<br/>");document.writeln("format(\"X{0}Y{1}Z{2}\", '1', '2') : "+ format("X{0}Y{1}Z{2}", '1', '2') + "<br/>");document.writeln("format(\"X{0}Y{1}Z{2}\", '1', '2', '3') : "+ format("X{0}Y{1}Z{2}", '1', '2', '3') + "<br/>");document.writeln("format(\"X{0}Y{1}Z{2}\", '1', '2', '3', '4') : "+ format("X{0}Y{1}Z{2}", '1', '2', '3', '4') + "<br/>");document.writeln("<hr />");document.writeln("format() : "+ format() + "<br/>");document.writeln("format(\"X{0}Y{1}Z{2}\", null) : "+ format("X{0}Y{1}Z{2}", null) + "<br/>");document.writeln("format(null, '1') : "+ format(null, '1') + "<br/>");document.writeln("<hr />");document.writeln("format(\"{0{0}1{1}2{2}}\") : "+ format("{0{0}1{1}2{2}}") + "<br/>");document.writeln("format(\"{0{0}1{1}2{2}}\", 'x') : "+ format("{0{0}1{1}2{2}}", 'x') + "<br/>");document.writeln("format(\"{0{0}1{1}2{2}}\", 'x', 'y') : "+ format("{0{0}1{1}2{2}}", 'x', 'y') + "<br/>");document.writeln("format(\"{0{0}1{1}2{2}}\", 'x', 'y', 'z') : "+ format("{0{0}1{1}2{2}}", 'x', 'y', 'z') + "<br/>");

上面代碼存在參數順序的bug,感謝KimSoft提出的最佳化方案:http://blog.csdn.net/kimsoft/article/details/7860106

String.prototype.format = function(){      var args = arguments;      return this.replace(/\{(\d+)\}/g,                          function(m,i){              return args[i];          });  }    var a = "I Love {0}, and You Love {1},Where are {0}! {4}";  alert(a.format("You","Me"));  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.