I have such template “text %variable.key% text”. The variable.key is the variable.
And I need to rename such variables to “text “+variable.key+” text”, to make them work.
I have tried to do something like this:
var tpl = "text %variable.key% text";
tpl = tpl.replace(/%(.*?)%/, function(a,b) {
return eval('b');
});
but it also returns a string.
Can somebody tell me how to do this?
It’s easy to do without using
evalat all:If you want to get properties starting from
windowyou can just callgetValue("path.to.property"). If you want to start from some other root object, usegetValue.call(rootObject, "path.to.property").The function could also be adapted to take the root object as an optional first parameter, but the idea remains the same.
See it in action.
Important: This will not work on Internet Explorer < 9 because
Array.prototype.forEachwill not exist. You can fix that with