Standalone substitute

Standalone substitute

JavaScript

String.prototype.substitute = function(object, regexp){
    return this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
        if (match.charAt(0) == '\\') return match.slice(1);
        return (object[name] != null) ? object[name] : '';
    });
};

var message = 'Hello {name}'.substitute({
    name: 'Adrian'
});

console.log(message);