test(){ let form={ bar_rule_txt:'{spu}-{master_attr_value}-{slave_attr_alias}', bar_rule_result:'', spu:'JPK1575G', master_attr_value:'黑色', master_attr_alias:'C001', slave_attr_value:'XXL', slave_attr_alias:'S002', }; let bar_rule_txt = form.bar_rule_txt; const regex = /\{([^}]+)\}/g; // 匹配大括號裡面內容 let match,rule_attr=[]; while ((match = regex.exec(bar_rule_txt))) { //console.log(match[1]); // 輸出捕獲組內匹配的內容,即大括號內的內容 rule_attr.push(match[1]); } console.log(rule_attr);//列印正規表示式-匹配的內容 let propertyName = "form."+rule_attr[0]; console.log(propertyName);//form.spu let objname = eval(propertyName); console.log(objname);//JPK1575G },
轉: https://www.cnblogs.com/mxjx/p/3259455.html