470,636 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

replace '\' ?

Hi,
I would like to enter a folder path like this
C:\Documents and Settings\chucklarge\Desktop
and get this
C:\\Documents and Settings\\chucklarge\\Desktop\\

this is what i have tried but can not get it right

function preparePath(folderPath)
{ var len = folderPath.length;
if(folderPath[len - 1] != '\\')
{ folderPath += '\\';
}
return folderPath.replace(\\, '\\\');
}

sourceFolderPath = preparePath("C:\Documents and
Settings\chucklarge\Desktop");
Any Help is appreciated,
Chuck

Jul 20 '05 #1
2 1394
chuck clark wrote:
Hi,
I would like to enter a folder path like this
C:\Documents and Settings\chucklarge\Desktop
and get this
C:\\Documents and Settings\\chucklarge\\Desktop\\

this is what i have tried but can not get it right

function preparePath(folderPath)
{ var len = folderPath.length;
if(folderPath[len - 1] != '\\')
{ folderPath += '\\';
}
return folderPath.replace(\\, '\\\');
}

sourceFolderPath = preparePath("C:\Documents and
Settings\chucklarge\Desktop");
Any Help is appreciated,
Chuck


Here ya go... I think you are getting confused about escape characters.
Whenever you have a string with a \, there needs to be another
character, so "\\\\" evaluates to \\. In your function, you are
replacing \\ with "\\\" which is guaranteed to break, since you are
escaping the second double-quote, and the string never closes.

Also, in your test, you need to add \\ in the path, because it will be
evaluated as \ before you send it into the function. _THIS IS
DIFFERENT_ from if you are handed a string with a \ in it. The escape
character \ is only used by the inputting user.

Brian

function preparePath(folderPath)
{
if(folderPath[folderPath.length - 1] != "\\")
folderPath += "\\";

return folderPath.replace(/\\/g, "\\\\");
}

sourceFolderPath = preparePath("C:\\Documents and
Settings\\chucklarge\\Desktop");
Jul 20 '05 #2
chuck clark <ch***@no-spam.mail.utexas.edu> writes:
I would like to enter a folder path like this
C:\Documents and Settings\chucklarge\Desktop
and get this
C:\\Documents and Settings\\chucklarge\\Desktop\\


folderPath = folderPath.replace(/\\/g,"\\\\");

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Craig Keightley | last post: by
6 posts views Thread by JackpipE | last post: by
15 posts views Thread by =?Utf-8?B?TWlrZSAiWU9fQkVFIiBC?= | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.