0

I will replace c:\pictures\picture1.png to c:\\pictures\\picture1.png

i.e:

var data="c:\pictures\picture1.png"
data=data.raplace('\','\\');

in asp.net it can run with

data=data.replace('\\','\\\\');

when I use this method in jquery it replaced only the firs '\' character and it comes so:

c:\\pictures\picture1.png 

how can I replace all '\' characters

Ebrar Bayburt
  • 37
  • 1
  • 3
  • 9

3 Answers3

3

If you search for the \ using a regular expression, you can use the g flag at the end of the expression to indicate you want to do a "global" search.

Also, your example is off. Any time you want to use the literal \ you need to write it twice as in \\.

var data="c:\\pictures\\picture1.png"
data = data.replace(/\\/g,'\\\\')
Cory Gagliardi
  • 770
  • 1
  • 8
  • 13
2

you can perform a global replacement by using g..

The g modifier is used to perform a global match (find all matches rather than stopping after the first match).

.replace(/\\/g,'\\\\'));

data = data.replace(/\\/g,'\\\\')
sasi
  • 4,192
  • 4
  • 28
  • 47
1

Expressions will help you here: http://jsfiddle.net/jC8hM/

var data = "c:\\pictures\\picture1.png"

alert(data);
data = data.replace(/\\/g, "\\\\");

alert(data);

To write a single instance of "\" you need to write "\". So to write "\", you need "\\".

tymeJV
  • 103,943
  • 14
  • 161
  • 157