Javascript String format(o)

Description

Javascript String format(o)


String.prototype.format = function (o) {
    return this.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }/*from w  w w. j  av  a  2s  .  c  o m*/
    );
};

Javascript String format(o)

String.prototype.format = function(o){
    var self = this;
    for(var i in o)self = self.replace(new RegExp("\\$\\{" + i + "\\}", "g"), o[i]);
    return self;
};



PreviousNext

Related