String format (JavaScript)

String.prototype.format = function() {
    var a = arguments;
    return this.replace(/\{\d+\}/g, function(c){
        return a[c.match(/\d+/)];
    });
}
 
// usage:
var str = 'Hello, {0}! You are {1}!';
str.format('friend', 'awesome');
// Hello, friend! You are awesome!

P.S.: Вот как-то так :D

дядь Саша

hardcore javascript & php programmer, passionate reverse engineer

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.