1. <legend id='09qwz'><style id='09qwz'><dir id='09qwz'><q id='09qwz'></q></dir></style></legend>
        <tfoot id='09qwz'></tfoot>

        <i id='09qwz'><tr id='09qwz'><dt id='09qwz'><q id='09qwz'><span id='09qwz'><b id='09qwz'><form id='09qwz'><ins id='09qwz'></ins><ul id='09qwz'></ul><sub id='09qwz'></sub></form><legend id='09qwz'></legend><bdo id='09qwz'><pre id='09qwz'><center id='09qwz'></center></pre></bdo></b><th id='09qwz'></th></span></q></dt></tr></i><div id='09qwz'><tfoot id='09qwz'></tfoot><dl id='09qwz'><fieldset id='09qwz'></fieldset></dl></div>

          <bdo id='09qwz'></bdo><ul id='09qwz'></ul>
      1. <small id='09qwz'></small><noframes id='09qwz'>

      2. Javascript 等价于 python 的 .format()

        时间:2023-05-30

          <tbody id='PSeZL'></tbody>
        <tfoot id='PSeZL'></tfoot><legend id='PSeZL'><style id='PSeZL'><dir id='PSeZL'><q id='PSeZL'></q></dir></style></legend>
              • <small id='PSeZL'></small><noframes id='PSeZL'>

                <i id='PSeZL'><tr id='PSeZL'><dt id='PSeZL'><q id='PSeZL'><span id='PSeZL'><b id='PSeZL'><form id='PSeZL'><ins id='PSeZL'></ins><ul id='PSeZL'></ul><sub id='PSeZL'></sub></form><legend id='PSeZL'></legend><bdo id='PSeZL'><pre id='PSeZL'><center id='PSeZL'></center></pre></bdo></b><th id='PSeZL'></th></span></q></dt></tr></i><div id='PSeZL'><tfoot id='PSeZL'></tfoot><dl id='PSeZL'><fieldset id='PSeZL'></fieldset></dl></div>
                  <bdo id='PSeZL'></bdo><ul id='PSeZL'></ul>
                  本文介绍了Javascript 等价于 python 的 .format()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想要一个模仿 python .format() 函数的 javascript 函数

                  I would like a javascript function that mimics the python .format() function that works like

                  .format(*args, **kwargs)
                  

                  上一个问题为 '.format(*args) 提供了一个可能(但不完整)的解决方案

                  A previous question gives a possible (but not complete) solution for '.format(*args)

                  JavaScript 等效于 printf/string.format

                  我希望能够做到

                  "hello {} and {}".format("you", "bob"
                  ==> hello you and bob
                  
                  "hello {0} and {1}".format("you", "bob")
                  ==> hello you and bob
                  
                  "hello {0} and {1} and {a}".format("you", "bob",a="mary")
                  ==> hello you and bob and mary
                  
                  "hello {0} and {1} and {a} and {2}".format("you", "bob","jill",a="mary")
                  ==> hello you and bob and mary and jill
                  

                  我意识到这是一项艰巨的任务,但也许在某个地方有一个完整(或至少部分)的解决方案,其中也包括关键字参数.

                  I realize that's a tall order, but maybe somewhere out there is a complete (or at least partial) solution that includes keyword arguments as well.

                  哦,我听说 AJAX 和 JQuery 可能有这方面的方法,但我希望能够在没有所有开销的情况下做到这一点.

                  Oh, and I hear AJAX and JQuery possibly have methods for this, but I would like to be able to do it without all that overhead.

                  特别是,我希望能够将它与 google doc 的脚本一起使用.

                  In particular, I would like to be able to use it with a script for a google doc.

                  谢谢

                  推荐答案

                  更新:如果您使用的是 ES6,模板字符串的工作方式与 String.format 非常相似:https://developers.google.com/web/updates/2015/01/ES6-Template-字符串

                  UPDATE: If you're using ES6, template strings work very similarly to String.format: https://developers.google.com/web/updates/2015/01/ES6-Template-Strings

                  如果不是,以下适用于上述所有情况,其语法与 python 的 String.format 方法非常相似.下面是测试用例.

                  If not, the below works for all the cases above, with a very similar syntax to python's String.format method. Test cases below.

                  String.prototype.format = function() {
                    var args = arguments;
                    this.unkeyed_index = 0;
                    return this.replace(/{(w*)}/g, function(match, key) { 
                      if (key === '') {
                        key = this.unkeyed_index;
                        this.unkeyed_index++
                      }
                      if (key == +key) {
                        return args[key] !== 'undefined'
                        ? args[key]
                        : match;
                      } else {
                        for (var i = 0; i < args.length; i++) {
                          if (typeof args[i] === 'object' && typeof args[i][key] !== 'undefined') {
                            return args[i][key];
                          }
                        }
                        return match;
                      }
                    }.bind(this));
                  };
                  
                  // Run some tests
                  $('#tests')
                    .append(
                      "hello {} and {}<br />".format("you", "bob")
                    )
                    .append(
                      "hello {0} and {1}<br />".format("you", "bob")
                    )
                    .append(
                      "hello {0} and {1} and {a}<br />".format("you", "bob", {a:"mary"})
                    )
                    .append(
                      "hello {0} and {1} and {a} and {2}<br />".format("you", "bob", "jill", {a:"mary"})
                    );

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <div id="tests"></div>

                  这篇关于Javascript 等价于 python 的 .format()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:浮动子元素:溢出:隐藏或清除:两者? 下一篇:并非所有浏览器都支持 toLocaleString()?

                  相关文章

                  最新文章

                  • <bdo id='YbAPp'></bdo><ul id='YbAPp'></ul>
                    <tfoot id='YbAPp'></tfoot>
                    <i id='YbAPp'><tr id='YbAPp'><dt id='YbAPp'><q id='YbAPp'><span id='YbAPp'><b id='YbAPp'><form id='YbAPp'><ins id='YbAPp'></ins><ul id='YbAPp'></ul><sub id='YbAPp'></sub></form><legend id='YbAPp'></legend><bdo id='YbAPp'><pre id='YbAPp'><center id='YbAPp'></center></pre></bdo></b><th id='YbAPp'></th></span></q></dt></tr></i><div id='YbAPp'><tfoot id='YbAPp'></tfoot><dl id='YbAPp'><fieldset id='YbAPp'></fieldset></dl></div>

                    <legend id='YbAPp'><style id='YbAPp'><dir id='YbAPp'><q id='YbAPp'></q></dir></style></legend>

                      <small id='YbAPp'></small><noframes id='YbAPp'>