problem with String.replace in console verses web page

J

John Passaniti

Hopefully someone can point out what I'm doing wrong.

I find myself having to dynamically create HTML code, and have found
that the usual way you see to do this is an unreadable mess, like this:

blah('<span id="' + id + '"><a href="' + link + '">' + linkText +
'</a></span>');

So instead, I would like to do something more like the variable
interpolation in Perl and other languages:

blah(XXX('<span id="@id"><a href="@link">@linkText</a></span>'));

The XXX function here would interpolate the values of any variables
named with a @ prefix into the string. Why @? Why not?

So using my favorite JavaScript console (FireBug), I typed the following
and ran it:

x = "hello world";
y = 42;
z = "--@x/@y--"

z = z.replace(/@(\w+)/g, function (dummy, v) {
return eval(v);
});

alert(z);

And it works. The alert box shows "--hello world/42--" as expected.
Okay, so now let's turn it into a function:

x = "hello world";
y = 42;

function XXX(s) {
return s.replace(/@(\w+)/g, function (dummy, v) {
return eval(v);
});
}

alert(XXX("!!!@x~~~@y!!!"));

And again, it works. The alert box shows "!!!hello world~~~42!!!" as
expected. I should be able now to just drop this *same* code into a
<script> section of a web page, right?

Wrong. If I take the *same* code as my second example and plop it in a
web page, the alert does not show the same thing as when in FireBug's
console. It shows the same string passed to XXX, unchanged.

Other experimentation tells me that when placed in a web page, the
anonymous function to do the eval call is never invoked, which suggests
that the regular expression didn't match.

I've only tried this in FireFox 2 and IE 7, but if it doesn't work
properly in either of those, I really don't care about the other browsers.

My guess is there is a difference in the execution environment of
FireBug's console (or likely, any JavaScript console) and the execution
environment of a web page. The questions are what is that difference,
and how do I make my XXX function work properly?
 
G

Geoffrey Summerhayes

Okay, so now let's turn it into a function:

x = "hello world";
y = 42;

function XXX(s) {
return s.replace(/@(\w+)/g, function (dummy, v) {
return eval(v);
});
}

alert(XXX("!!!@x~~~@y!!!"));

I've only tried this in FireFox 2 and IE 7, but if it doesn't work
properly in either of those, I really don't care about the other browsers.

Can't replicate the failure, works fine in both for me.
 
J

John Passaniti

Geoffrey said:
Can't replicate the failure, works fine in both for me.

I'm starting to see the problem.

In my testing, I had tried to run that code inside an existing web
page-- a page that prior to my call brought in jQuery and a handful of
other JavaScript libraries. When I stripped things down to bare
minimum, it worked. It's very likely my problem is that one of those
other JavaScript libraries is mucking around with String.replace in some
odd way.

Grrr.
 
G

Geoffrey Summerhayes

I'm starting to see the problem.

In my testing, I had tried to run that code inside an existing web
page-- a page that prior to my call brought in jQuery and a handful of
other JavaScript libraries. When I stripped things down to bare
minimum, it worked. It's very likely my problem is that one of those
other JavaScript libraries is mucking around with String.replace in some
odd way.

Grrr.

Just as well, my reply to your email wouldn't go through.

Technical details of permanent failure:
PERM_FAILURE: SMTP Error (state 13): 550 <[email protected]>:
Recipient address rejected: User unknown in virtual alias table
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top