Connecting Tech Pros Worldwide Help | Site Map

problem with String.replace in console verses web page

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 17th, 2007, 05:15 AM
John Passaniti
Guest
 
Posts: n/a
Default problem with String.replace in console verses web page

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?

  #2  
Old May 17th, 2007, 04:15 PM
Geoffrey Summerhayes
Guest
 
Posts: n/a
Default Re: problem with String.replace in console verses web page

On May 17, 1:11 am, John Passaniti <put-my-first-name-
h...@JapanIsShinto.comwrote:
Quote:
>
<*snip*>
Quote:
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*>
Quote:
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

  #3  
Old May 17th, 2007, 07:15 PM
John Passaniti
Guest
 
Posts: n/a
Default Re: problem with String.replace in console verses web page

Geoffrey Summerhayes wrote:
Quote:
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.
  #4  
Old May 17th, 2007, 08:15 PM
Geoffrey Summerhayes
Guest
 
Posts: n/a
Default Re: problem with String.replace in console verses web page

On May 17, 3:05 pm, John Passaniti <n...@JapanIsShinto.comwrote:
Quote:
Geoffrey Summerhayes wrote:
Quote:
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 <nntp@japanisshinto.com>:
Recipient address rejected: User unknown in virtual alias table

---
Geoff

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.