replace+eval

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
 
E

Evertjan.

Cristian Tarsoaga wrote on 18 apr 2005 in comp.lang.javascript:

I'm a javascript begginner, and I wrote a small piece of code that uses
the replace and the eval methods.

eval() is evil, never use it, You don't need it.
See the archive of this NG.

in a browser ude the window[] global identifier:

function f(x) {return window[x+"VALUE"];}

elswhere or everywhere use self[]

function f(x) {return self[x+"VALUE"];}

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));

You can .replace with a function,
but a string argument of that function
cannot be modified by the $1 output.

[and you should have defined betaVALUE]
 
C

Cristian Tarsoaga

Thanks Evertjan.

Still, I cannot fully use this information, but maybe I'm missing something!
I tried to write it as


alfaVALUE = 9;
betaVALUE = 17;
myText = "alfaVAR and betaVAR and even more goes here";
myText.replace(/(\w*)VAR/g, window[$1+"VALUE"]);



and still, some error occurs.
I also didn't understood what you meant by saying that
You can .replace with a function,
but a string argument of that function
cannot be modified by the $1 output.

I will use the code inside a browser.

Thanks for your help, maybe you still can give me some hint

Chris
 
L

Lasse Reichstein Nielsen

Evertjan. said:
eval() is evil, never use it, You don't need it.
See the archive of this NG.

Hear, hear.
in a browser ude the window[] global identifier:

function f(x) {return window[x+"VALUE"];}

elswhere or everywhere use self[]

"self" is also a browser property. It refers to the current window,
corresponding to the HTML target "_self".

A safe way to get the global object is:
var global = (function(){return this;})();
Then use that.
....
Notice how the second argument is a string in the first case. The
substring "$1" has a specific meaning inside such a pattern.
In the second case, the "$1" is not inside a string, so it's used
as a variable, an undefined one at that.

In any case, even if "$1" meant something, the value of "f($1)" is
calculated once, before calling "replace". That will not give you
different results for the two matches.

To make a computed replacement, you can use a function argument:

result = myString.replace(/(\w*)VAR/g,
function(match,submatch,index) {
return f(submatch);
});

/L
 
E

Evertjan.

Cristian Tarsoaga wrote on 18 apr 2005 in comp.lang.javascript:
Thanks Evertjan.

Still, I cannot fully use this information, but maybe I'm missing
something! I tried to write it as


alfaVALUE = 9;
betaVALUE = 17;
myText = "alfaVAR and betaVAR and even more goes here";
myText.replace(/(\w*)VAR/g, window[$1+"VALUE"]);



and still, some error occurs.
I also didn't understood what you meant by saying that

As I said, you simply cannot do that $1 is a part of a textlitteral,
not a javascript variable in itself, and the textlitteral has to be the
second parametere of .replace() itself.

What you want is impossible this way!!!

======================

Test:

<script type='text/javascript'>

alfaVALUE = 9;
betaVALUE = 17;
myText = "alfaVAR and betaVAR and even more goes here";

r=myText.replace(/(\w*)VAR/g, window["$1VALUE"]);

document.write(r+'<br>');
// undefined and undefined and even more goes here

r=myText.replace(/(\w*)VAR/g, "$1VALUE");

document.write(r);
// alfaVALUE and betaVALUE and even more goes here

</script>

=======================

The best you can do is this:

=======================

<script type='text/javascript'>

alfaV = 9;
betaV = 17;
myText = "alfaVAR and betaVAR and even more goes here";

r=
myText.replace(/alfaVAR/g,alfaV).replace(/betaVAR/g,betaV);

document.write(r);
// 9 and 17 and even more goes here

</script>
 
D

Douglas Crockford

Cristian said:
I'm a javascript begginner, and I wrote a small piece of code that uses
the replace and the eval methods.

function f(x) {return eval(x+"VALUE");}

Who taught you to use eval that way? Do you have an incompetent teacher?
Do you have a book from an incompetent author?

The wicked must be punished!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top