Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String replacement only replaces first instance.

Status
Not open for further replies.

thessa

Programmer
Jun 18, 2001
21
IT
IE5.
This string replacement function only replaces the first instance of an asterisk with a backslash instead of all asterisks in the string. Anyone have ideas as to why it's not replacing all the *?

<SCRIPT LANGUAGE=&quot;JavaScript&quot; TYPE=&quot;text/javascript&quot;>
<!--
function sendPath(pathName){
var newpath = pathName.replace(&quot;*&quot;,&quot;\\&quot;);
window.opener.document.forms[0].elements[0].value=newpath;
self.close;
}
//-->;

Background if you will find it helpful:
I am passing a file path in a variable to a javascript function, but I couldn't make the backslashes (\) pass in the string. I decided instead to replace the slashes with asterisks before sending the variable, and now the asterisks get passed inside the variable, so that the file path 'C:\Inetpub\ looks like this 'C:*Inetpub* when it arrives in the javascript function. So far so good. Then I try to replace all of the asterisks with backslashes again so that it will look like a normal file path and I can pass the value to a text box on the parent page. But instead of replacing all asterisks, it only replaces the first one, like this: 'C:\Inetpub*
thanks
thessa
 
function swap()
{for (i=0;i<document.form.field.value.length;i++)
{document.form.field.value = document.form.field.value.replace(&quot;8&quot;,&quot;*&quot;)}}


This will repeat the replacement for each character in the path, ie 25 characters 25 replacements.

Hope it helps. DeltaFlyer
The Only Programmer To Crash With Style. LOL
 
Thanks so much for the quick reply! I used your advice and it works perfectly! I changed the function around to fit my scenario like this:

function sendPath(pathName){
var newpath = pathName.replace(&quot;*&quot;,&quot;\\&quot;);
for (i=0;i<pathName.length;i++)
{newpath = newpath.replace(&quot;*&quot;,&quot;\\&quot;);
}
window.opener.document.forms[0].elements[0].value=newpath;
window.close;
}

have a good one
 
No Problem, Glad to help. DeltaFlyer
The Only Programmer To Crash With Style. LOL
 
Another solution is a to use a regular expression.

var newpath = pathName.replace(/\*/g,&quot;\\&quot;);

Then you only have to call it once, no need for a loop. jared@eae.net -
 
right on, Jared! Hope Thessa comes back to read your reply. Hope the DeltaFlyer reads it too, come to think of it.
 
The DeltaFlyer has entered the building. (Hiya JohnBoy)

Jared, if your still linked to this thread, can you help me out, i know from one of your previous threads that regular expressions are some form of higher language, Java,C type stuff can't remember, but could you point me to a good site that will educate me in their wily ways.

Thanks as always, DeltaFlyer
The Only Programmer To Crash With Style. LOL
 
Yes, even better plan, the regular expression! I have implemented it and it's short, sweet and running fine. I'll be looking for those references DeltaFlyer suggested, as I have read a bit on regular expressions but still don't have a good grasp on them...
 
Jared, what a star you are.
Cheers buddy. DeltaFlyer
The Only Programmer To Crash With Style. LOL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top