Created
October 17, 2009 02:06
-
-
Save andrewwatts/212206 to your computer and use it in GitHub Desktop.
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
/** | |
* a very crude implmentation of python's format() for javascript, | |
* supporting simple replacement fields, eg:: | |
* | |
* >>> format('{0} is {1}', 'javascript', 'cool') | |
* "javascript is cool" | |
* | |
* works for my purposes, but no where close to implementing full | |
* capabilities as described at:: | |
* http://docs.python.org/library/string.html#formatstrings | |
* | |
*/ | |
function format(){ | |
var formatted_str = arguments[0] || ''; | |
for(var i=1; i<arguments.length; i++){ | |
var re = new RegExp("\\{"+(i-1)+"}", "gim"); | |
formatted_str = formatted_str.replace(re, arguments[i]); | |
} | |
return formatted_str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment