{"id":1026,"date":"2023-03-12T09:36:35","date_gmt":"2023-03-12T01:36:35","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1026"},"modified":"2023-04-29T13:13:19","modified_gmt":"2023-04-29T05:13:19","slug":"js-string-formatting-method","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/js-string-formatting-method\/","title":{"rendered":"JS\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5"},"content":{"rendered":"<h2>\u6269\u5c55String\u539f\u578b\u5bf9\u8c61\u65b9\u6cd5<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">String.prototype.format = function (args) {\n  var result = this;\n  if (arguments.length &gt; 0) {\n    if (arguments.length == 1 &amp;&amp; typeof (args) == &quot;object&quot;) {\n      for (var key in args) {\n        if (args[key] != undefined) {\n          var reg = new RegExp(&quot;({&quot; + key + &quot;})&quot;, &quot;g&quot;);\n          result = result.replace(reg, args[key]);\n        }\n      }\n    }\n    else {\n      for (var i = 0; i &lt; arguments.length; i++) {\n        if (arguments[i] != undefined) {\n          var reg = new RegExp(&quot;({)&quot; + i + &quot;(})&quot;, &quot;g&quot;);\n          result = result.replace(reg, arguments[i]);\n        }\n      }\n    }\n  }\n  return result;\n}<\/code><\/pre>\n<h2>\u6d4b\u8bd5<\/h2>\n<pre><code>var str = &#039;\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u7684\u5b57\u7b26\u4e32\uff1a{0} {1}&#039;.format(&#039;Hello&#039;,&#039;world&#039;);\n\/\/\u6216\u8005\nvar str = &#039;\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u7684\u5b57\u7b26\u4e32\uff1a{str0} {str1}&#039;.format({str0:&#039;Hello&#039;, str1:&#039;world&#039;});<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u7684\u5b57\u7b26\u4e32\uff1aHello world<\/p>\n<pre><code>var url = &#039;http:\/\/www.appblog.cn\/articles\/{start}\/{count}&#039;.format({ start: 0, count: 10 })<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.appblog.cn\/articles\/0\/10\">http:\/\/www.appblog.cn\/articles\/0\/10<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6269\u5c55String\u539f\u578b\u5bf9\u8c61\u65b9\u6cd5 String.prototype.format = function (args [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[84],"class_list":["post-1026","post","type-post","status-publish","format-standard","hentry","category-frontend-basic","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1026"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1026\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}