Our application needs to process a file path (ie. snip off the last element) which we get in non escaped form c:\blah\di\blah. Unfortunately the backslashes are not escaped, like c:\\blah\\di\\blah, which means that javascript does not really recognise the backslahes; calling a function like indexOf(\\) will return -1.
I have seen countless questions on stackOverflow but I am at a loss on how to proceed.
There is no way we can change the data we get because it is from another framework (extJS),
Is there some way of replacing the single backslashes with \\ ? Then we could process it properly.
I tried :
- str.replace(/\/, "\\\\");
- str.replace(/\/g, "\\\\"); mentioned here.
- str.replace(String.fromCharCode(92),String.fromCharCode(92,92)); mentioned here
- str.replace("\\", "\\\\");
- escape() function mentioned here. This gives me c%3A%08lahdi%08lah
- In the same question this looked promising: var justTheName = str.split(String.fromCharCode(92)).pop(); But also does not work for me.
I have a fiddle with this code :
var str = 'c:\blah\di\blah'
str = str.replace(/\\/, "\\\\");
alert('' + str)