Javascript中的Sprintf(字符串格式)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript中的Sprintf(字符串格式)相关的知识,希望对你有一定的参考价值。

Example:

`var result = "Hello {0}! This is {1}.".format("world","foo bar");`

Returns:

`"Hello World! This is foo bar."`
  1. String.prototype.format = function(){
  2. var pattern = /{d+}/g;
  3. var args = arguments;
  4. return this.replace(pattern, function(capture){ return args[capture.match(/d+/)]; });
  5. }

以上是关于Javascript中的Sprintf(字符串格式)的主要内容,如果未能解决你的问题,请参考以下文章