Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace \ with \\ using javascript string functions

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"
like image 493
mahesh Avatar asked Dec 29 '22 06:12

mahesh


1 Answers

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.

like image 187
Andy E Avatar answered Dec 31 '22 15:12

Andy E