string format

Format a string with '{0}','{1}'...

当前为 2022-10-29 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/453846/1110629/string%20format.js

  1. // ==UserScript==
  2. // @name string format
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Format a string with '{0}','{1}'...
  6. // @license MIT
  7. // @author 捈荼
  8. // ==/UserScript==
  9.  
  10. (function () {
  11. "use strict";
  12.  
  13. if (String.prototype.format == undefined) {
  14. String.prototype.format = function () {
  15. var args = arguments;
  16. var cnt = 0;
  17. return this.match(/{(\d+)}/g) == null && this.match(/{}/g) != null ?
  18. this.replace(/{}/g, (match) => {
  19. return typeof args[cnt] != 'undefined' ? args[cnt++] : match;
  20. }) :
  21. this.replace(/{(\d+)}/g, (match, number) => {
  22. return typeof args[number] != 'undefined' ? args[number] : match;
  23. });
  24. }
  25. } else {
  26. throw 'String.prototype.format defined.';
  27. }
  28. })();