string format

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

目前為 2022-10-28 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/453846/1110441/string%20format.js

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