Last active
September 12, 2016 16:32
-
-
Save jmelosegui/8aaab0f21d48f85956e4ab9c72b3feb1 to your computer and use it in GitHub Desktop.
Mimicking the .Net string format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This will format a string mimicking the .Net string format. | |
ie: format('test {0} - {1}', 'value 1', 'value 2'); | |
*/ | |
function format(value) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
return value.replace(/{(\d+)}/g, function (match, number) { | |
return typeof args[number] != 'undefined' | |
? args[number] | |
: match | |
; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment