By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

problem with String.replace in console verses web page

P: n/a
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
<scriptsection 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?
May 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 17, 1:11 am, John Passaniti <put-my-first-name-
h...@JapanIsShinto.comwrote:
>
<*snip*>
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!!!"));
<*snip*>
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.

---
Geoff

May 17 '07 #2

P: n/a
Geoffrey Summerhayes wrote:
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.
May 17 '07 #3

P: n/a
On May 17, 3:05 pm, John Passaniti <n...@JapanIsShinto.comwrote:
Geoffrey Summerhayes wrote:
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.
Just as well, my reply to your email wouldn't go through.

Technical details of permanent failure:
PERM_FAILURE: SMTP Error (state 13): 550 <nn**@japanisshinto.com>:
Recipient address rejected: User unknown in virtual alias table

---
Geoff

May 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.