473,327 Members | 2,025 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,327 software developers and data experts.

I can not get a strings replace() method to work

I need to change imageUploadInfo2 to caption2 where the number at the
end is a variable that needs to be preserved (imageUploadInfo1 to
caption1, imageUploadInfo43 to caption43, etc)

Can anyone tell me why the following code doesn't work? In the alert at
the bottom I still get "imageUploadInfo2" (which starts off in
idOfInputThatNeedsToHaveItsImageInputUpdated).
idOfInputWhoseCaptionWeWillUpdate =
idOfInputThatNeedsToHaveItsImageInputUpdated;
var textToChange = "imageUploadInfo";
var regX = new RegExp(textToChange, "i");
idOfInputWhoseCaptionWeWillUpdate.replace(regX, "caption");
alert("the input name is " + idOfInputWhoseCaptionWeWillUpdate);


I've also forgotten if Javascript passes values by value or by copy. I
assume by value, or my code is going to become a horrible mess.

Sep 7 '06 #1
9 1334
Jake Barnes wrote:
I need to change imageUploadInfo2 to caption2 where the number at the
end is a variable that needs to be preserved (imageUploadInfo1 to
caption1, imageUploadInfo43 to caption43, etc)

Can anyone tell me why the following code doesn't work? In the alert at
the bottom I still get "imageUploadInfo2" (which starts off in
idOfInputThatNeedsToHaveItsImageInputUpdated).
idOfInputWhoseCaptionWeWillUpdate =
idOfInputThatNeedsToHaveItsImageInputUpdated;
var textToChange = "imageUploadInfo";
var regX = new RegExp(textToChange, "i");
idOfInputWhoseCaptionWeWillUpdate.replace(regX, "caption");
alert("the input name is " + idOfInputWhoseCaptionWeWillUpdate);
You need to capture the return value of the replace function. It does
not replace the text inside the string on which it called; rather, it
returns a new string containing the old string with the specified
replacement performed.

This is probably what you want:

idOfInputWhoseCaptionWeWillUpdate =
idOfInputWhoseCaptionWeWillUpdate.replace(regX, "caption");
>

I've also forgotten if Javascript passes values by value or by copy. I
assume by value, or my code is going to become a horrible mess.
"By value" and "by copy" are essentially the same. Did you mean "By
value or by reference"?

Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.

Jeremy
Sep 7 '06 #2
Jeremy wrote:
[...]
Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.
Maybe confused :-)

In an assignment expression, the value of the right hand side is
assigned to the left hand side. If the value is a primitive, then that
is assigned. If its value is a reference to an object, then a
reference is assigned.

e.g.

var x = 2; // x is assigned the value of 2
var y = x; // y is assigned the value of x, which is 2

var foo = {}; // foo is assigned a reference to an object
var bar = foo; // bar is assigned the value of foo,
// a reference to the same object
bar = y; // bar is now assigned the value of y, which is 2

HTH
--
Rob

Sep 7 '06 #3

RobG написав:
Jeremy wrote:
[...]
Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.

Maybe confused :-)

In an assignment expression, the value of the right hand side is
assigned to the left hand side. If the value is a primitive, then that
is assigned. If its value is a reference to an object, then a
reference is assigned.

e.g.

var x = 2; // x is assigned the value of 2
var y = x; // y is assigned the value of x, which is 2

var foo = {}; // foo is assigned a reference to an object
var bar = foo; // bar is assigned the value of foo,
// a reference to the same object
bar = y; // bar is now assigned the value of y, which is 2

HTH
BTW it passes complex data type (objects/arrays) by reference and
primitive data types (string/number/boolean etc.) by value. Check the
JS specification.

Sep 7 '06 #4
scriptg...@gmail.com wrote:
RobG написав:
Jeremy wrote:
[...]
Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.
Maybe confused :-)

In an assignment expression, the value of the right hand side is
assigned to the left hand side. If the value is a primitive, then that
is assigned. If its value is a reference to an object, then a
reference is assigned.

e.g.

var x = 2; // x is assigned the value of 2
var y = x; // y is assigned the value of x, which is 2

var foo = {}; // foo is assigned a reference to an object
var bar = foo; // bar is assigned the value of foo,
// a reference to the same object
bar = y; // bar is now assigned the value of y, which is 2

HTH
BTW it passes complex data type (objects/arrays) by reference and
primitive data types (string/number/boolean etc.) by value. Check the
JS specification.
Is that intended to be a correction or a further explanation? The "="
operator is a simple assignment operator, not a "pass" opertator. It
assigns values, it doesn't "pass" them.

Check the ECMAScript Language Specification, Section 11.3.
--
Rob

Sep 7 '06 #5

RobG написав:
scriptg...@gmail.com wrote:
RobG написав:
Jeremy wrote:
[...]
Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.
>
Maybe confused :-)
>
In an assignment expression, the value of the right hand side is
assigned to the left hand side. If the value is a primitive, then that
is assigned. If its value is a reference to an object, then a
reference is assigned.
>
e.g.
>
var x = 2; // x is assigned the value of 2
var y = x; // y is assigned the value of x, which is 2
>
var foo = {}; // foo is assigned a reference to an object
var bar = foo; // bar is assigned the value of foo,
// a reference to the same object
bar = y; // bar is now assigned the value of y, which is 2
>
HTH
>
BTW it passes complex data type (objects/arrays) by reference and
primitive data types (string/number/boolean etc.) by value. Check the
JS specification.

Is that intended to be a correction or a further explanation?
It was a shorter explanation.

var a=[1,2,3]
function x(arr){
arr.pop()
}
x(a)

Var passed by reference.

Sep 7 '06 #6
sc********@gmail.com wrote:
RobG напи ав:
<snip>
> var foo = {}; // foo is assigned a reference to an object
var bar = foo; // bar is assigned the value of foo,
// a reference to the same object
<snip>
BTW it passes complex data type (objects/arrays) by reference
and primitive data types (string/number/boolean etc.) by value.
Check the JS specification.
ECMA 262 states that both the right hand side of an assignment and the
Expressions in an arguments list are evaluated and the result passed as
an argument to the internal - GetValue - function, with the returned
value being the value assigned or the value used as the argument to a
function call. The specification also never states what the 'value' of
an object actually is. However, the practical upshot of this is that all
assignments are by value, and all function arguments are passed by
value, it is just that some of those values effectively 'refer' to
objects.

Richard.
Sep 8 '06 #7
sc********@gmail.com wrote:
RobG написав:
scriptg...@gmail.com wrote:
RobG написав:
Jeremy wrote:
[...]
Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.

Maybe confused :-)

In an assignment expression, the value of the right hand side is
assigned to the left hand side. If the value is a primitive, then that
is assigned. If its value is a reference to an object, then a
reference is assigned.

e.g.

var x = 2; // x is assigned the value of 2
var y = x; // y is assigned the value of x, which is 2

var foo = {}; // foo is assigned a reference to an object
var bar = foo; // bar is assigned the value of foo,
// a reference to the same object
bar = y; // bar is now assigned the value of y, which is 2

HTH

BTW it passes complex data type (objects/arrays) by reference and
primitive data types (string/number/boolean etc.) by value. Check the
JS specification.
Is that intended to be a correction or a further explanation?
It was a shorter explanation.

var a=[1,2,3]
function x(arr){
arr.pop()
}
x(a)

Var passed by reference.
Sorry, but I find such explanations confusing and unhelpful - I'd
rather use the terms and expressions contained in the language
specification. If someone doesn't understand how an assignment
operator works, the above example won't help them at all.

In your use of:

var a = [1,2,3];

you should explain that "a" is assigned a reference to the Array object
created on the right. You might then have said that:

var b = a;

will result in b being assigned the same value as a, so that now b is a
reference to the same array. So the value of a variable is always
"passed by value". Saying that some are "passed by reference" and that
others are "passed by value" leads to a fundamental misunderstanding of
how assignment works and of what the value of a variable is.

I don't know where the term "passed" came from originally, but the use
of terminology from some other context assumes that the person
implicitly understands the same (unstated) context. They may well
spend more time trying to learn and apply the foreign terminology to an
inappropriate situation than in understanding the answer the original
question.

Recent discussions on JavaScript Objects and hash tables spring to
mind. :-)
--
Rob

Sep 8 '06 #8

Jeremy wrote:
Jake Barnes wrote:
I've also forgotten if Javascript passes values by value or by copy. I
assume by value, or my code is going to become a horrible mess.

"By value" and "by copy" are essentially the same. Did you mean "By
value or by reference"?

Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.
Yes, thank you, that is what I meant. Been dabbling with Ruby lately,
forgetting which languages pass what.

Sep 8 '06 #9
RobG wrote:
Jeremy wrote:
[...]
>Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.

Maybe confused :-)

In an assignment expression, the value of the right hand side is
assigned to the left hand side. If the value is a primitive, then that
is assigned. If its value is a reference to an object, then a
reference is assigned.

e.g.

var x = 2; // x is assigned the value of 2
var y = x; // y is assigned the value of x, which is 2

var foo = {}; // foo is assigned a reference to an object
var bar = foo; // bar is assigned the value of foo,
// a reference to the same object
bar = y; // bar is now assigned the value of y, which is 2

HTH

Hmph! You're right! I just tested it out. I had always thought
anonymous objects were copied on assignment. I guess it's never been an
issue.
Sep 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Hellas | last post by:
Hello guy I'm italian boy, I need help for doing a strange thing with numbers I have a number from operation like this: a = float("010.123") b = float("001.124") p= a*b p="%06.3f" % p the...
12
by: Barnes | last post by:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form? Here is the scenario: I have a form that cannot accept apostrophes. I want to use the replace() so...
5
by: Colin Savage | last post by:
Please could somebody explain what an "atomized string" is? I have been doing loops in the msdn around XmlNameTable and NameTable but neither explain what an atomized string is. The example for one...
6
by: Brett | last post by:
I've been trying different methods for replacing escaped strings. None work. I even have a custom Replace() method but still can't get what I need. Some of the characters I need to replace are:...
6
by: Christian Blackburn | last post by:
Hi Gang, When encoding HTML strings it'll convert things like " --> &rsquo and the like using Server.HTMLEncode(). However, is there a command to make sure strings don't contain valid SQL...
8
by: DierkErdmann | last post by:
Hi ! I know that this topic has been discussed in the past, but I could not find a working solution for my problem: sorting (lists of) strings containing special characters like "", "",......
4
by: JJ | last post by:
Is there a way of checking that a line with escape sequences in it, has no strings in it (apart from the escape sequences)? i.e. a line with \n\t\t\t\t\t\t\t\r\n would have no string in it a...
1
by: Andrus | last post by:
I have a lot of localizable strings enclosed in double quotes in cs source files. I'm looking for a tool which extracts all those strings for localizaton to a single file. Or even better, it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.