473,383 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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?
May 17 '07 #1
3 1765
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: DLP22192 | last post by:
I have the following single-line if statement that is evaluating true even though it shouldn't. I have never seen this before and I am concerned that this can happen in other areas of my code. ...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
0
by: Tim Wagaman | last post by:
I an having issuses with a loop I am running to keep checking for messages coming across our line. The goal: Listen for messages on port 5001 and print the messages into a text file. The port...
16
by: BBM | last post by:
This is so bizarre I hesitate to post it, but I need to get this working. My code looks like this... Private Const cmdTestResourcesSel = "SELECT * FROM TResources" & _ " WHERE Scenario =...
21
by: Phil Powell | last post by:
<script type=\"text/javascript\"> <!-- var browserArray = new Array(\"Microsoft Internet Explorer\", \"Netscape\"); if (browserArray.in_array(navigator.appName, browserArray) &&...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
0
by: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.