I need to send the folder path to MySQL. So I have escaped each backslash. The path to be sent via javascript will be :
\\\\192.168.0.1\\foo\\bar
This will ultimately be saved as (in db)
:
\\192.168.0.1\foo\bar
So, I wrote the following code:
fpath = fpath.replace(/\\+/g,'\\\\'); // Replace multiple occurences of backslash with double backslash
var fpathLen = fpath.length;
if(fpath.charAt(fpathLen-1) == '\\')
fpath = fpath.substring(0,fpathLen-2); // Remove the trailing backslash if any
if(fpath.charAt(0) == '\\') {
fpath = '\\\\' + fpath; // Add 2 more backslash before sending the string to MySQL
}
else
fpath = '\\\\\\\\' + fpath; // If string is without a leading backslash, insert 4 backslash
But it doesn't seem to be an effective way of doing things! For example, I have manually added 4
, in another case 8
backslashes to make it 2
and 4
respectively on js
side. Is there a better way of achieving this?
Edit:
For an input path like fpath = \\1.1.1.1\\\\\\\\foo\bar\\\config
, output should be \\\\192.168.0.1\\foo\\bar