1

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)
Community
  • 1
  • 1
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • when you do ```console.log(str.split('').map(function(ch){ return ch.charCodeAt(0);}));```, you get ```[ 99, 58, 8, 108, 97, 104, 100, 105, 8, 108 ... ``` which means ```\b``` and ```\d``` give the same value ```8``` , that is your roadblock, not sure how to overcome that, are you reading all of this from a file, if so then there is hope... – mido Nov 18 '14 at 10:21
  • No, unfortunately this is supplied within the Javascript code by the extjs framework – Oliver Watkins Nov 18 '14 at 10:26
  • `var str = 'c:\blah\di\blah'` is already "c:lahdilah". There's no way to replace a backslash that doesn't exist. – William Barbosa Nov 18 '14 at 10:38
  • Correct me if I'm wrong, but when ExtJs gives you a string with backslashes in it, doesn't that mean that the backslashes are already escaped? If so, your test is wrong, and you should work with a correctly escaped test-string. – Yoshi Nov 18 '14 at 10:40
  • Extjs gives me a string "c:\blah\di\blah". Whether or not they are escaped or not is irrelevant. The string is not in a state where I can snip off the last part. – Oliver Watkins Nov 18 '14 at 10:47
  • You need to initialize string as: var str = 'c:\\blah\\di\\blah' – Max Nov 18 '14 at 10:50
  • 1
    My point is: If you have a valid string-var with a value that when logged shows "c:\blah\di\blah" and no other errors given, you don't have to concern yourself with *fixing* that string, because it is already a valid value. You only have to correctly split it in your code, e.g.: `str.split('\\').pop()`. Meaning it's your code where you have to handle the escaping correctly, not with the runtime values. – Yoshi Nov 18 '14 at 10:50
  • so you are saying if I SEE it in the console or debugger with 1 slash it means it really has 2 slashes in it? – Oliver Watkins Nov 18 '14 at 11:33
  • Yes, though it's technically not correct, you can think about it that way. For js a backslash is a backslash. Though for the interpreter, when parsing code, a backslash is the escape-character which needs to be escaped when one wants to use it in literal values. E.g. `var foo = 'C:\\';` same as with `str.split('\\').pop()`. The point is: "there is no double-backslash" (pun intended). – Yoshi Nov 18 '14 at 11:35
  • alright i think I sort of got it - extjs is actually giving me the right values.. (Maybe its just me, but this backslash business confuses the heck out of me.) – Oliver Watkins Nov 18 '14 at 11:57
  • 1
    Well, you're not the first, and you'll surely not be the last to stumble upon this topic ;) If possible, think about answering your question, so that future visitors can profit from it. – Yoshi Nov 18 '14 at 12:10

0 Answers0