JQuery String Format

I like this simple jquery format code, by gpvos, and I am adding it to my library.  It makes string formatting easier.

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;
};

'{0} {0} {1} {2}'.format(8.11, 'a{2}bc', 'def');

// output:
// 8.11 8.11 a{2}bc def

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s