-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

Ramesh
  • 1,829
  • 2
  • 25
  • 30
  • This one is asked multiple times before. Like http://stackoverflow.com/q/14624295/508702 and http://stackoverflow.com/q/8684975/508702 – Bas Slagter Jul 01 '13 at 11:07
  • you can see out put not getting ... please see alert it alert D:ImagingEMail31844529039.pdf instead of D:\\Imaging\\EMail\\31844529039.pdf – Ramesh Jul 01 '13 at 11:10

2 Answers2

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • You put two backslash in the string literal, and there's only one in the resulting string. – Denys Séguret Jul 01 '13 at 11:07
  • you can see out put not getting ... please see alert it alert D:ImagingEMail31844529039.pdf instead of D:\\Imaging\\EMail\\31844529039.pdf – Ramesh Jul 01 '13 at 11:08
2

Use

str = str .replace(/\\/g, "\\\\");
rags
  • 2,580
  • 19
  • 37