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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I replace \ in a string with \\

Status
Not open for further replies.

mmyyyy

Technical User
Apr 17, 2008
2
CA
Hello,

if I have a string "c:\folder1\file1.txt"
how do I convert the above string to "c:\\folder1\\file1.txt" by using javascript.
thanks in advance for your help.
 
[tt]s="c:\\folder1\\file1.txt"; //input
t=s.replace(/\\/g,"\\\\"); //output
[/tt]
 
Hi tsuji

thanks for the reply.

my goal was to replace c:\folder1\file1.txt with c:\\folder1\\file1.txt

not to replace c:\\folder1\\file1.txt with c:\\folder1\\file1.txt.

 
It is doing what you're asking.
[tt]
s="c:\\folder1\\file1.txt"; //input
t=s.replace(/\\/g,"\\\\"); //output
alert(s+"\n"+t);

//if you like and that it risks to confuse you more
s="c:\x5cfolder1\x5cfile1.txt"; //input
t=s.replace(/\\/g,"\\\\"); //output
alert(s+"\n"+t);
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top