C
Cristian Tarsoaga
Hi there!
I'm a javascript begginner, and I wrote a small piece of code that uses
the replace and the eval methods.
When calling them separately, they seem to work and everything is ok.
But when combining them, nothing happens.
So, that's the code:
alfaVALUE = 9;
//step 1
function f(x) {return eval(x+"VALUE");}
f("alfa");
//returns 9, OK for me
//step 2
myString = 'alfaVAR and betaVAR';
myString.replace(/(\w*)VAR/g, "$1VALUE");
//replaces with "alfaVALUE and betaVALUE", OK for me
//step 3, I'd like to use the values stored in alfaVALUE and betaVALUE
result = myString.replace(/(\w*)VAR/g, f($1));
//???
Thanks in advance
Chris
I'm a javascript begginner, and I wrote a small piece of code that uses
the replace and the eval methods.
When calling them separately, they seem to work and everything is ok.
But when combining them, nothing happens.
So, that's the code:
alfaVALUE = 9;
//step 1
function f(x) {return eval(x+"VALUE");}
f("alfa");
//returns 9, OK for me
//step 2
myString = 'alfaVAR and betaVAR';
myString.replace(/(\w*)VAR/g, "$1VALUE");
//replaces with "alfaVALUE and betaVALUE", OK for me
//step 3, I'd like to use the values stored in alfaVALUE and betaVALUE
result = myString.replace(/(\w*)VAR/g, f($1));
//???
Thanks in advance
Chris