Javascript: Replace backslashes with double backslashes

14,190

WSParameters.replace(/\\/g, "\\\\"); should do it, and in FF18 as well. Notice that if you use JSON.stringify, this is done automatically. Also watch out that many console outputs (Firebug etc) do surround string contents with quotes, but do not escape them.

Share:
14,190
ArgisIsland
Author by

ArgisIsland

Updated on June 04, 2022

Comments

  • ArgisIsland
    ArgisIsland almost 2 years

    I'm currently having the problem mentioned in the title and I'm somehow not finding a way to properly replace backsashes with double backslashes, so that I can properly give the string to a webservice as parameters. Let me show you what I tried. Some of these do actually work for some other people, but not for me... I'm currently testing this with FF18.0.1

    WSParameters.replace(/\\/g, "\\\\\\\\");
    WSParameters.replace("\\", "\\\\\\\\");
    WSParameters.replace(/\\/g, "\\\\");
    WSParameters.replace(/\\/g, "\\");
    WSParameters.replace(/\\/g, "\");
    WSParameters.replace("\\", "\\\\");
    

    Thanks a lot in advance

    EDIT: I should mention that it's somehow parsed into JSON and with firebug I see the backslash in the source string, but not in the JSON view. Maybe there is another way? But somehow it's already failing at the replacement of the backslashes.

    EDIT2:

    if (noAction == false) {
        $.ajax({
            type: "POST",
            url: "WebService.asmx/" + webMethod,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: pAsync,
            data: WSParameters,
            success: function callFunction(result) { processPOSTResults(result, pType, pNot);},
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error while communicating with WebAdmin web service. - ' + xhr.status + " " + thrownError);
            }
        });
    }
    
  • ArgisIsland
    ArgisIsland about 11 years
    Thanks Bergi, I don't use JSON stringify, please se my EDIT 2 for the JSON WS part.
  • Bergi
    Bergi about 11 years
    However maybe you should use it for posting JSON? What is WSParameters, how do you get it?
  • ArgisIsland
    ArgisIsland about 11 years
    The WSParameters variable is a string and looks like this: {param1: 'abc', param2: 'def', list1: ['4711','4712']}
  • ArgisIsland
    ArgisIsland about 11 years
    I'll have a look at this, thanks. But is this also possible with dynamically generated lists and nested lists?
  • Bergi
    Bergi about 11 years
    How did you get that string? It's not valid JSON at all.
  • ArgisIsland
    ArgisIsland about 11 years
    I built it based on the experience I made, this was the only format tht worked for me.
  • Bergi
    Bergi about 11 years
    Your webservice only accepts javascript object literals, no JSON???
  • ArgisIsland
    ArgisIsland about 11 years
    I guess it also accepts JSON, but I couldn't figure out the correct use and then went with the more convenient format. However it seems that this was the wrong decision.
  • Bergi
    Bergi about 11 years
    Yes. Use an object, and JSON.stringify (like here).
  • ArgisIsland
    ArgisIsland about 11 years
    Just wanted to let you know that using the JSON format and JSON.stringify solved the problem with the backslash in the web service parameters. Thanks again!