List of utility methods to do String Format
formatString(input)String.prototype.formatString = function(input) { return this.replace(/(?:#{(\w+)})/g, function($0, $1) { return input[$1]; }); }; var options = { name: 'John' }; console.log('Hello, there! Are you #{name}?'.formatString(options)); ... | |
formatString(num, comma)Number.prototype.formatString = function(num, comma) { return this.toFixed(num).toString().replace(/\.([0-9]*)$/, comma+"$1"); | |
format()String.prototype.format = String.prototype.f = function () { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); return s; }; | |
format()String.prototype.format = String.prototype.f = function () { var args = arguments; return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) { if (m == "{{") { return "{"; } if (m == "}}") { return "}"; } return args[n]; }); }; | |
format()String.prototype.format = String.prototype.f = function () { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); return s; }; | |
format()String.prototype.format = String.prototype.f = function() { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); return s; }; | |
format()String.prototype.format = String.prototype.f = function() { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); return s; }; | |
format()'use strict'; String.prototype.format = String.prototype.f = function() { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); return s; }; ... | |
format(options)String.prototype.format = String.prototype.f = function(options) { options = options ? options : []; if (typeof options != "object") options = [options]; var s = this, i = options.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), options[i]); return s; ... | |
format()String.prototype.format = String.prototype.format = function() { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); return s; }; |