-2

I have a string with an escape character:

var a = "Hello\ World";

I want to split the string into an array of characters which includes the escape character:

var a = "Hello\ World";
/* Do something here */
a; // I want output: ["H","e","l","l","o","\"," ","W","o","r","l","d"]

Typically I split the string using:

a=a.split('');

But of course this does not work because the escape character is ignored during the split.

var a = "Hello\ World";
a=a.split('');
a; // outputs ["H","e","l","l","o"," ","W","o","r","l","d"]

Now I know in order to get the results I want I have to use a double escape. I know that my string should look like:

var a = "Hello\\ World";

But that is not the string I have, I am not typing in the string manually it is generated. So I fully understand that I need a double escape but I can not manually create one. I need to know how to programmatically transform "Hello\ World" into "Hello\\ World".

Is there some magical function that will escape all backslashes? Is there a doubleEscape() function I am not aware of, or maybe there is some regex replace function that can help me out.

I do not want the answer "use a double escape" or "your string needs to be 'Hello\ World'. I am fully aware that this is what I need to do but I need to programmatically escape the escape character, it can not be done manually.

I have attempted something like this:

var a = "Hello\ World";
a = a.replace("\", "\\");

But of course this doesn't work because the escape character is ignored during the replace. I have tried things like:

var a = "Hello\ World";
a = a.replace("\\", "\\\\");

But this gives an error. I believe it is interpreting the search parameter as a regex, which is an invalid regex, which causes an error.

Another post that was similar suggested this:

var a = "Hello\ World";
a = a.replace(/\\/g, '\\\\');
a; // outputs "Hello World";

This does not work. The string remains as if the replace was not executed.

Any suggestions?

Dustin Poissant
  • 3,201
  • 1
  • 20
  • 32
  • 1
    `'\ '` is equivalent to `' '`, which means that programmatically what you have is exactly equivalent to `'Hello World'`, and there's absolutely no way of knowing where the `\` character was in the string (unless you parse the source code for that specific string). – zzzzBov Feb 11 '15 at 00:30
  • Yeah i see that answer like 4 times but I'm not any closer to a real answer. – Dustin Poissant Feb 11 '15 at 00:31
  • 3
    I'm voting to close this question as off-topic because it is based on a fallacy and a fundamental misunderstanding – user229044 Feb 11 '15 at 00:42
  • 2
    @DustinPoissant There *is* no real answer. Your question is completely unanswerable. – user229044 Feb 11 '15 at 00:42

2 Answers2

4

No, you can't. It's not that the backslash is ignored in the split.

Instead, the problem is that the backslash isn't in the string:

"Hello\ World"; // "Hello World"

Therefore, once the string literal has been parsed, you can't recover the slash.

However, ECMAScript 6 introduces template strings. And with String.raw, you can recover the raw string form:

`Hello\ World`;           //  "Hello World"
String.raw`Hello\ World`; //  "Hello\ World"
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • I don't have a raw string. I have a regular one. How would i convert ``a`` into a raw string? – Dustin Poissant Feb 11 '15 at 00:26
  • @DustinPoissant If you use `\`` instead of quotes, you will have a template string. – Oriol Feb 11 '15 at 00:28
  • The string is being passed into a function as a parameter. Sense javascript does not allow me to specify a parameter type i can not force the caller to pass in a string raw. – Dustin Poissant Feb 11 '15 at 00:30
  • 2
    @DustinPoissant Your question makes no sense. Do you have the literally snippet of code `var a = "Hello\ World"` or don't you? This is actually important if you want an answer. It is impossible for that string to be just "passed in" to your function, because it is impossible for that string to exist outside of source code. The string isn't `"Hello\ World"`, the string is `"Hello World"`. The backslash is present in the source code *only*. – user229044 Feb 11 '15 at 00:44
  • So you are telling me its impossible to call ``func("Hello\ World")`` because I m pretty sure I can lol. I am 100% sure that I can pass ``Hello\ World`` into a function with no problem the problem is then, from within the function, determining where the escape character use to be. Which is what I want. The question is not stupid but it's possible that JavaScript can not do what I am asking. I am asking to detect where the escape character use to be, if JavaScript can not do it than say so, but don't try and tell me its a stupid question when it's a completely legitimate question. – Dustin Poissant Feb 11 '15 at 00:56
  • The answer may well be "When the string is parsed the escape character is removed and it is then impossible to determine where it use to be", if that is the answer then fine, just thought I would ask a legitimate question and see if anyone has ever came across this issue before and found a solution. – Dustin Poissant Feb 11 '15 at 00:59
0

I tried this method: I ran your code and variations. To get around the problem of escaping backlashes, I though to make the function myself by testing for the CharCode. The function output was index=5 as expected.

function f(x) {
    var a = -1,i,n=x.length;
    for(i=0;i<n;i++)
    {
        if(x.charAt(i)==String.fromCharCode(92))
        {
            a=i;
            break;
        }
    }
    return a.toString();
}

function h(x) {
    document.getElementById('in').innerHTML = x;
    document.getElementById('out').innerHTML = f(x);
}

<button type="button"
onclick="h('Hello\\ World')">
Run</button>

<p id="in"></p>
<hr>
<p id="out"></p>
Bacchus
  • 121
  • 2