Reputation: 37
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
Upvotes: 0
Views: 2357
Reputation: 4318
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,'\\\\')
Upvotes: 2
Reputation: 104785
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 "\\".
Upvotes: 1
Reputation: 780
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,'\\\\')
Upvotes: 3