supasympa
9/26/2013 - 8:26 AM

Replace a string with model object properties

Replace a string with model object properties

var M = function(){
}

M.prototype = {
    format : function(){
            var s = "/data/mu/programs/{{programCode}}/campaign-mus/{{muid}}/validate";
            var r = /{{[a-zA-Z_-]*}}/gi.exec(s);
            var key = '';
                
            while(r!==null){
                key = r[0].replace('{{','').replace('}}','');
                s = s.replace(r[0], this[key]);
                
                r = /{{[a-zA-Z_-]*}}/gi.exec(s);
            }
        
        return s;
    }
}

var m = new M();

m.programCode = 'YETI';
m.muid = 9999;

m.format();