473,486 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

replace+eval

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
Jul 23 '05 #1
5 2235
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]

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
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
Jul 23 '05 #3
"Evertjan." <ex**************@interxnl.net> writes:
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.
....
myString.replace(/(\w*)VAR/g, "$1VALUE"); .... result = myString.replace(/(\w*)VAR/g, f($1));


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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
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>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #5
Cristian Tarsoaga wrote:
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!
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
4464
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
4
1907
by: J. J. Cale | last post by:
given that I have a js file included which is written programatically and I can't change it. I would like to know how to do the following using something other than the deprecated eval(). whats in...
4
27226
by: tascienu | last post by:
consider this code below: var toremove = "some"; var tolook ="I am looking for some other text in this string toremove"; if i do this: tolook = tolook.replace(/toremove/gi,''); will to...
4
9008
by: Angel | last post by:
Hello Everybody, I have the following lines in my code 1) totalElements=eval("document."+formname+".RCBillingCycle"+totalRCRows+".length") 2) var newoption=new Option...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.