-4

Syntax error.

var str = "D:\Imaging\EMail\31844529039.pdf";
str = str.replace(/\/g, "\\");
alert(str);

Please help required output is D:\\ImagingEMail\\31844529039.pdf

2

2 Answers 2

4

The \ is an escaping sign both in string literals and in regex literals, so you must escape it. Use

var str = "D:\\Imaging\\EMail\\31844529039.pdf";
str = str.replace(/\\/g, "\\\\");

This builds this string :

D:\\Imaging\\EMail\\31844529039.pdf

Special characters in String literals are described here.

Special characters in regular expression literals are described here.

Sign up to request clarification or add additional context in comments.

2 Comments

You put two backslash in the string literal, and there's only one in the resulting string.
you can see out put not getting ... please see alert it alert D:ImagingEMail31844529039.pdf instead of D:\\Imaging\\EMail\\31844529039.pdf
2

Use

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.