Need help with a simple problem

S

SteveB

However, despite being simple, I can't figure it out (maybe because I'm a
newbie).

I'm trying to replace a string with another string, but it simply is not
working (no error messages, just won't work).

Basically, I have a document and I'd like to every occurrence of '&&1&&'
with 'test'. Eventually the user will specify what '&&1&&' will be replaced
with, but for now I can't get this simple test to work. What am I doing
wrong?

Thanks!

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!--
var sDocument = new String("This is a &&1&&.");

alert (sDocument);

sDocument.replace("&&1&&", "test");
// sDocument.replace("&&1&&", txtPrompt.value);

alert (sDocument);

//-->
</SCRIPT>
 
L

Lee

SteveB said:
However, despite being simple, I can't figure it out (maybe because I'm a
newbie).

I'm trying to replace a string with another string, but it simply is not
working (no error messages, just won't work).

the String.replace() method doesn't change the value of the string.
It creates a new string, with the replacement made:

var string1="alpha";
var string2=string1.replace("alph","bet");
 
S

SteveB

Thanks Lee, worked like a charm!

Lee said:
SteveB said:

the String.replace() method doesn't change the value of the string.
It creates a new string, with the replacement made:

var string1="alpha";
var string2=string1.replace("alph","bet");
 
S

SteveB

One more question if I may. The replace function only seems to replace the
first instance of the string it's looking for. What if there are multiple
instances (for example, more than one &&1&& in my example), how can I
replace them all (keep in mind that I don't know how many there might be)?

Thanks!
 
L

Lee

SteveB said:
One more question if I may. The replace function only seems to replace the
first instance of the string it's looking for. What if there are multiple
instances (for example, more than one &&1&& in my example), how can I
replace them all (keep in mind that I don't know how many there might be)?

The first argument to replace is more correctly a "regular expression",
rather than a simple string. Regular expressions allow such things
as wildcards and specifying that the replacement should be done
"globally", throughout the string:

string2=string1.replace(/alph/g,"bet");

The "g" indicates "global" replacement.

Beware that some characters ("^[/", and others) have special
meaning within regular expressions.
 
S

SteveB

Once again... thank you Lee! That did the trick!

Lee said:
SteveB said:
One more question if I may. The replace function only seems to replace the
first instance of the string it's looking for. What if there are multiple
instances (for example, more than one &&1&& in my example), how can I
replace them all (keep in mind that I don't know how many there might
be)?

The first argument to replace is more correctly a "regular expression",
rather than a simple string. Regular expressions allow such things
as wildcards and specifying that the replacement should be done
"globally", throughout the string:

string2=string1.replace(/alph/g,"bet");

The "g" indicates "global" replacement.

Beware that some characters ("^[/", and others) have special
meaning within regular expressions.
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top