754 source | link edited Jun 29 '16 at 16:25 Adriaan 15.7k 7 7 gold badges 35 35 silver badges 66 66 bronze badges

Inline Side-by-side Side-by-side Markdown Building on the previously suggested solutions: // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET") outputs ASP is dead, but ASP.NET is alive! ASP {2} If you prefer not to modify String 's prototype: if (!String.format) { String.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } Gives you the much more familiar: String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET'); with the same result: ASP is dead, but ASP.NET is alive! ASP {2} Building on the previously suggested solutions: // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET") outputs ASP is dead, but ASP.NET is alive! ASP {2} If you prefer not to modify String 's prototype: if (!String.format) { String.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } Gives you the much more familiar: String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET'); with the same result: ASP is dead, but ASP.NET is alive! ASP {2} Building on the previously suggested solutions: // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET") outputs ASP is dead, but ASP.NET is alive! ASP {2} If you prefer not to modify String 's prototype: if (!String.format) { String.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } Gives you the much more familiar: String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET'); with the same result: ASP is dead, but ASP.NET is alive! ASP {2}

753 source | link edit approved Jun 24 '16 at 18:08 haykam 399 10 10 silver badges 20 20 bronze badges

Inline Side-by-side Side-by-side Markdown Building on the previously suggested solutions: // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET") outputs ASP is dead, but ASP.NET is alive! ASP {2} If you prefer not to modify String 's prototype: if (!String.format) { String.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } Gives you the much more familiar: String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET'); with the same result: ASP is dead, but ASP.NET is alive! ASP {2} Building on the previously suggested solutions: // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET") outputs ASP is dead, but ASP.NET is alive! ASP {2} If you prefer not to modify String 's prototype: if (!String.format) { String.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } Gives you the much more familiar: String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET'); with the same result: ASP is dead, but ASP.NET is alive! ASP {2} Building on the previously suggested solutions: // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET") outputs ASP is dead, but ASP.NET is alive! ASP {2} If you prefer not to modify String 's prototype: if (!String.format) { String.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } Gives you the much more familiar: String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET'); with the same result: ASP is dead, but ASP.NET is alive! ASP {2}

Notice removed Content dispute by Community ♦ occurred Feb 7 '14 at 15:48

Post Unlocked by Community ♦ occurred Feb 7 '14 at 15:48

Post Locked by Brad Larson ♦ occurred Feb 6 '14 at 15:33

Notice added Content dispute by Brad Larson ♦ occurred Feb 6 '14 at 15:33

Mod Removes Wiki by Brad Larson ♦ occurred Feb 6 '14 at 15:31

752 source | link edited Feb 6 '14 at 15:31 Brad Larson ♦ 167k 45 45 gold badges 385 385 silver badges 560 560 bronze badges

Inline Side-by-side Side-by-side Markdown // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; var sprintfRegex = /\{(\d+)\}/g; var sprintf = function (match, number) { return number in args ? args[number] : match; }; return this.replace(sprintfRegex, sprintf); }; } // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } if (!String.format) { String.format = function (format) { var args = Array.prototype.slice.call(arguments, 1); var sprintfRegex = /\{(\d+)\}/g; var sprintf = function (match, number) { return number in args ? args[number] : match; }; return format.replace(sprintfRegex, sprintf); }; } if (!String.format) { String.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; var sprintfRegex = /\{(\d+)\}/g; var sprintf = function (match, number) { return number in args ? args[number] : match; }; return this.replace(sprintfRegex, sprintf); }; } if (!String.format) { String.format = function (format) { var args = Array.prototype.slice.call(arguments, 1); var sprintfRegex = /\{(\d+)\}/g; var sprintf = function (match, number) { return number in args ? args[number] : match; }; return format.replace(sprintfRegex, sprintf); }; } // First, checks if it isn't implemented yet. if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; } if (!String.format) { String.format = function(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; }

751 source | link edited Feb 6 '14 at 14:06 ckozl 6,545 3 3 gold badges 29 29 silver badges 49 49 bronze badges

750 source | link edited Feb 6 '14 at 14:06 fearphage 15.9k 1 1 gold badge 24 24 silver badges 31 31 bronze badges

749 source | link edited Feb 6 '14 at 13:53 ckozl 6,545 3 3 gold badges 29 29 silver badges 49 49 bronze badges

748 source | link edited Feb 6 '14 at 13:53 fearphage 15.9k 1 1 gold badge 24 24 silver badges 31 31 bronze badges

747 source | link edited Feb 6 '14 at 13:53 ckozl 6,545 3 3 gold badges 29 29 silver badges 49 49 bronze badges

746 source | link edited Feb 6 '14 at 13:53 fearphage 15.9k 1 1 gold badge 24 24 silver badges 31 31 bronze badges

745 source | link edited Feb 6 '14 at 13:52 ckozl 6,545 3 3 gold badges 29 29 silver badges 49 49 bronze badges

744 source | link edited Feb 6 '14 at 13:52 fearphage 15.9k 1 1 gold badge 24 24 silver badges 31 31 bronze badges

743 source | link edited Feb 6 '14 at 13:38 ckozl 6,545 3 3 gold badges 29 29 silver badges 49 49 bronze badges

742 source | link edited Feb 6 '14 at 13:38 fearphage 15.9k 1 1 gold badge 24 24 silver badges 31 31 bronze badges

741 source | link edited Feb 6 '14 at 13:36 ckozl 6,545 3 3 gold badges 29 29 silver badges 49 49 bronze badges

740 source | link edited Feb 6 '14 at 13:36 fearphage 15.9k 1 1 gold badge 24 24 silver badges 31 31 bronze badges

739 source | link edited Feb 6 '14 at 13:35 ckozl 6,545 3 3 gold badges 29 29 silver badges 49 49 bronze badges

738 source | link edited Feb 6 '14 at 13:35 fearphage 15.9k 1 1 gold badge 24 24 silver badges 31 31 bronze badges

737 source | link edited Feb 6 '14 at 13:35 ckozl 6,545 3 3 gold badges 29 29 silver badges 49 49 bronze badges

736 source | link edited Feb 6 '14 at 13:35 fearphage 15.9k 1 1 gold badge 24 24 silver badges 31 31 bronze badges