views:

39

answers:

4

I have the following filepath

var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"

how can i replace \ with \ so so that it prints

var imagepath="C:\\Documents and Settings\\Mahesh\\Desktop\\images\\advts.png"
+1  A: 

You need to use the replace() method whilst escaping the \ character on each occurrence:

imagepath = imagepath.replace(/\\/g, "\\\\");
//-> "C:\\Documents and Settings\\Mahesh\\Desktop\\images\\advts.png"

If imagepath is defined with single backslash characters, those will be evaluated into an escape sequence along with the character(s) following them. At this point it's too late to replace the backslashes as they are removed from the resulting string.

Andy E
Thanks for the reply ,why are you using four \\\\ over here
mahesh
@mahesh: In JS string and regex literal grammar, \ is an escape character. As such, it needs to be escaped itself. Each literal \ you require in a string or regex needs another \ before it.
Andy E
Code mentioned here does not insert \\ instead it replaces \ with empty like C:Documents and SettingsMaheshDesktopimagesadvts.png
mahesh
@mahesh: re-read the last part of my answer and/or reko_t's answer. It isn't this code that's removing those slashes, they aren't in the string to begin with. If your file path is defined like it is in your question, each `\` needs an extra `\` in front of it, like in the second example of your question - `C:\\Documents and Settings\\...`.
Andy E
+1  A: 

It's a string literal.. By the time it's assigned to imagepath, the escape sequences are already parsed and processed, and the backslashes no longer exist in the string. You just have to have them in the string in the first place.

To demonstrate what I mean:

>>> var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
>>> imagepath
"C:Documents and SettingsMaheshDesktopimagesadvts.png"

There's no way to fix the issue at that stage.

reko_t
+1  A: 

You can use the string replace() method. Replace on W3Schools.com.

Example:

var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
document.write(imagepath.replace("\\", "\\\\"));

Upate As reko_t said, once a path is assigned to imagepath, it's a processed literal. The \ characters disappears. I've just tested my code with this example and there's no way to fix it unfortunately.

<html>
    <head>
        <script type="text/javascript">
            function init() {
                var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
                alert(imagepath.replace(/\\/g, "\\\\"));
            }
        </script>
    </head>
    <body onload="init()">

    </body>
</html>

The only way to do it, is to replace \ to \\ on server side, and assign it to imagepath before it gets rendered to javascript that needs to be used by javascript.

The Elite Gentleman
This won't work, see my answer for the explanation of why.
reko_t
I saw that...thanks....
The Elite Gentleman
If it did work, it would only replace the first backslash. For global replaces you need to use a regular expression.
Andy E
@Andy E, thanks, my updated post uses regular expression to show that either my first or second solution won't work....
The Elite Gentleman
A: 

Just add after var imagepath initialisation

var imagepath=imagepath.replace("\", "\\");
Wazzy