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

Need help with a simple problem

P: n/a
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>
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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");

Jul 20 '05 #2

P: n/a
Thanks Lee, worked like a charm!

"Lee" <RE**************@cox.net> wrote in message
news:bn*********@drn.newsguy.com...
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");

Jul 20 '05 #3

P: n/a
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!

"Lee" <RE**************@cox.net> wrote in message
news:bn*********@drn.newsguy.com...
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");

Jul 20 '05 #4

P: n/a
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.

Jul 20 '05 #5

P: n/a
Once again... thank you Lee! That did the trick!

"Lee" <RE**************@cox.net> wrote in message
news:bn*********@drn.newsguy.com...
SteveB said:

One more question if I may. The replace function only seems to replace thefirst 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.

Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.