Javascript equivalent to python#39;s .format()(Javascript 等价于 python 的 .format())
问题描述
我想要一个模仿 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Javascript 等价于 python 的 .format()
基础教程推荐
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01