JavaScript相当于Python的.format() [英] Javascript equivalent to python's .format()

查看:219
本文介绍了JavaScript相当于Python的.format()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  .format(* args ,** kwargs)

前面的问题给出了一个可能的(但不是完整的)'.format (* args)



JavaScript相当于printf / string.format



我希望能够做到

$ $ p $格式(you,bob
==>你好你和bob

hello {0格式(you,bob)
==> hello你和bob

hello {0}和{1}和{a} .format(you,bob,a =mary)
==>&hello you and bob and mary

hello {0} and {1} and格式(你,bob,jill,a =mary)
==>你好,bob和mary和jill

我意识到这是一个很高的顺序,但也许有一个地方是一个包含关键字参数的完整(或至少部分)解决方案。



哦,我听说AJAX和JQuery可能有这方面的方法,但我想能够做到这一点没有所有的开销。

特别是,我想能够使用它与谷歌文档的脚本。



谢谢 解析方案

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



如果不是,那么下面的例子适用于上面的所有情况,与python的 String.format 方法非常相似。测试用例如下。

String.prototype.format = function(){var args =参数; this.unkeyed_index = 0;返回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) );}; //运行一些测试$('#tests').append(hello {} and {}< br />\".format(\"you,bob)).append(hello {0}和{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 /你,bob,jill,{a:mary}));

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


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

.format(*args, **kwargs)

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

JavaScript equivalent to printf/string.format

I would like to be able to do

"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.

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.

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

Thanks

解决方案

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

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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆