Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Interpolate Arrays Inside Regular Expressions???

1 view
Skip to first unread message

Craig

unread,
Mar 18, 1998, 3:00:00 AM3/18/98
to

The following regular expression looks at "BadWord[i]" literally. I need
JavaScript to interpolate it as a variable. Any suggestions?

A = A.replace(/BadWord[i]/gi,GoodWord[i]);

Regards,

Craig Dansie
Dansie Website Design and CGI programming
HTML - Perl - JavaScript
Get a free trial website today!
http://www.dansie.net
des...@dansie.net

Christopher Thompson

unread,
Mar 20, 1998, 3:00:00 AM3/20/98
to

Craig (des...@dansie.net) wrote:
: The following regular expression looks at "BadWord[i]" literally. I need

: JavaScript to interpolate it as a variable. Any suggestions?

: A = A.replace(/BadWord[i]/gi,GoodWord[i]);

Try using eval().

--

-=Christopher Thompson=-

Spam filtered out with SpamBeGone! http://www.internz.com/SpamBeGone/
comp.lang.javascript Mini-FAQ at http://www.dannyg.com/ JavaScript
FAQ at http://www.btinternet.com/~martin.webb/faq.html
My minimalistic web site: http://ugweb.cs.ualberta.ca/~thompson/


Message has been deleted

Nick Fitzsimons

unread,
Mar 27, 1998, 3:00:00 AM3/27/98
to

Mike Shaver wrote:
>
> Christopher Thompson wrote:
> > Try using eval().
>
> No! Don't use eval! Eval is evil, and should be used only in the
> direst of circumstances! Bad Christopher!

Mike, I know that eval was buggy or totally non-functional on certain NN
2 versions/platforms, but I'd never thought of it as being actually
*evil* - can you expand a little on your chastisement of Christopher, as
although I almost never use eval myself, I don't want to stray too far
from the path of righteousness - well, not in my coding, anyway :-)

TIA,
Nick Fitz.
--
Nick Fitzsimons
Atlas Interactive Ltd
Aberdeen / Leicester
Scotland / England
UK

http://www.atlasinteractive.com

mailto:nickfit...@atlasinteractive.com


jason olmsted

unread,
Mar 27, 1998, 3:00:00 AM3/27/98
to

On Fri, 27 Mar 1998 13:52:38 +0000, Nick Fitzsimons
<nickfit...@atlasinteractive.com> wrote:

>Mike Shaver wrote:
>>
>> Christopher Thompson wrote:
>> > Try using eval().
>>
>> No! Don't use eval! Eval is evil, and should be used only in the
>> direst of circumstances! Bad Christopher!
>
>Mike, I know that eval was buggy or totally non-functional on certain NN
>2 versions/platforms, but I'd never thought of it as being actually
>*evil* - can you expand a little on your chastisement of Christopher, as
>although I almost never use eval myself, I don't want to stray too far
>from the path of righteousness - well, not in my coding, anyway :-)
>

Who knows what crawled up Mike's ass in this statement, but I know I
wouldn't categorize eval as evil (maybe he liked the pun :-). It has
been quite annoying for some who did not use it correctly with respect
to the platform, or who overused it, but it can be really handy at
times.

First, elaboration of the above

1) IE - must be the full line. There are so many variations to this
damn browser that I am sure there are exceptions, but the safe rule is
to only use eval for a complete statement.

2) Overuse - eval is apparently much slower than it ought to be, I
haven't run any benchmarks, but others have indicated this to be so,
so overuse means performance hits. Bad enough that the browser may be
living on a 486 with <= 16M Ram that you shouldn't add insult to
injury.

That said, eval can come in handy for some key things:

1) Backward compatibility with the void function

function myVoid(stm) {
eval(stm);
}

2) Variable variables
for (i=0;i<5;i++) {
eval ("var var" +i+ " = " i);
}

On the other hand, arrays can be used to achieve a similar effect, but
it isn't exactly the same.

var vars = new Array();
for (i=0;i<5;i++) {
vars[i] = i;
}

And, eval can come in handy with image swapping, but, there again,
arrays can be used with success as well.

The gist is that while they aren't evil, they should be used, as Mike
indicated, with moderation.

jason olmsted
mailto:olm...@phat-media.com
http://www.phat-media.com/olm/

Nick Fitzsimons

unread,
Mar 30, 1998, 3:00:00 AM3/30/98
to

Yeah, that's pretty much the conclusion I'd come to. When I started
programming with JS about 18 months ago, one of the first resources I
came across was a FAQ for NN 2 which referred to eval being broken on
some implementation or other. (See, Catherine, some of us know what to
do before posting :-) Since then I've never used it in production code
(even though I don't bother overmuch about NN 2 these days); although I
*very* occasionally find a use for it during development, there always
seems to be some workaround.

As far as IE goes, I agree entirely. Something like 70% of my early
learning curve involved developing stuff which worked fine on NN 3, but
bombed on MSIE 3. After a couple of months, I switched to developing on
MSIE and testing with NN later - NN usually had no problem with code
that worked for IE. It wasn't a desirable way of working (although the
customer specified MSIE 3 as standard for its ISP division, presumably
because it was free, everybody in the division I was working with used
NN), but it's easier to fix IE problems as you go along rather than
trawling through the whole application!

I also figured that the performance hit must be in there, but hadn't
realised that it might impact so heavily. Last time I tested NN3 on a
486 DX/33 with 8 Mb, Win 3.11 and a DriveSpaced HD, it took nearly five
minutes for the browser to start up anyway, so I think eval is probably
worth working around.

jason olmsted wrote:
>
> On Fri, 27 Mar 1998 13:52:38 +0000, Nick Fitzsimons
> <nickfit...@atlasinteractive.com> wrote:
>

<snip>
> Although I almost never use eval myself, I don't want to stray too far


> >from the path of righteousness - well, not in my coding, anyway :-)
> >
>
> Who knows what crawled up Mike's ass in this statement, but I know I
> wouldn't categorize eval as evil (maybe he liked the pun :-). It has
> been quite annoying for some who did not use it correctly with respect
> to the platform, or who overused it, but it can be really handy at
> times.
>
> First, elaboration of the above
>
> 1) IE - must be the full line. There are so many variations to this
> damn browser that I am sure there are exceptions, but the safe rule is
> to only use eval for a complete statement.
>
> 2) Overuse - eval is apparently much slower than it ought to be, I
> haven't run any benchmarks, but others have indicated this to be so,
> so overuse means performance hits. Bad enough that the browser may be
> living on a 486 with <= 16M Ram that you shouldn't add insult to
> injury.
>

<snip useful stuff>

> The gist is that while they aren't evil, they should be used, as Mike
> indicated, with moderation.
>
> jason olmsted
> mailto:olm...@phat-media.com
> http://www.phat-media.com/olm/

--

0 new messages