Javascript:用双反斜杠替换反斜杠 [英] Javascript: Replace backslashes with double backslashes

查看:1148
本文介绍了Javascript:用双反斜杠替换反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在标题中提到了问题,我以某种方式找不到使用双反斜杠正确替换backsashes的方法,以便我可以将该字符串正确地作为参数提供给webservice。
让我告诉你我试过的东西。其中一些确实适用于其他一些人,但不适用于我...我正在使用FF18.0.1进行测试。

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("\\", "\\\\");

提前感谢很多

编辑:我应该提到它以某种方式解析成JSON,并且在firebug中,我看到源字符串中的反斜杠,但不是在JSON视图中。也许还有另一种方式?但是不知何故,在更换反斜杠时已经失败了。

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);
        }
    });
}


推荐答案

WSParameters.replace(/ \\ / g,\\\\); 应该这样做,在FF18中也是这样。请注意,如果您使用 JSON.stringify ,则会自动完成。还要注意,许多控制台输出(Firebug等)会用引号围绕字符串内容,但不要将其退出。

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.

这篇关于Javascript:用双反斜杠替换反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆