I have a JSON object that I use to send name: value form parameters, and some of the object values have references to another object between curly braces. I want to regex replace any such tags with the object value they reference.
I've tried using regular expressions to replace /\{([^}]+)\}/g with obj['$1'] but it doesn't work.
var objArg = {
custom: [],
id: 188746,
name: "Miscellany",
format: "private",
section: "service",
material: ["internal"],
rack_plan_product_attributes: {
id: 531074,
product_attributes: {
id: 635366
}
},
make_up_policy_attributes: {
id: 324855
}
}
var s = 'The name is {name} and the rack plan product attributes id is {rack_plan_product_attributes.id}.';
s = s.replace(/\{([^}]+)\}/g, objArg['$1']);
Maybe I need to use a function like s = s.replace(/\{([^}]+)\}/g, function(r) { something... } but I don't really understand how that works. I'm expecting the result to make s into The name is Miscellany and the rack plan product attributes id is 531074.