【问题标题】:Escaping and unescaping a string with single and double quotes in Javascript在Javascript中使用单引号和双引号转义和取消转义字符串
【发布时间】:2012-10-25 22:55:58
【问题描述】:

我有一个搜索框,用户可以搜索任何字符串,包括单引号和双引号,一旦他们搜索,后端会将关键字传回给我,这样我就可以把它放回框中。我不知道字符串是什么,所以我自己无法转义引号,下面是一个示例:

var keyword = "hello";
$("#selectionkeywords").val();

我遇到的问题是,如果用户输入“hello”,则关键字变为“”hello””,我收到此错误:

missing ) after argument list
[Break On This Error]   
jQuery("#selectionkeywords").val(""hello"");

用户也可以输入单引号,这样也可以排除它。我尝试使用 escape unescape 但我仍然有同样的问题,例如逃脱(“你好”) 我可以以非转义格式获取值,例如“你好”,但我不知道如何处理它,escape 不起作用我最终得到这个 %26%23034%3Bhello%26%23034%3B

所以我现在几乎被困住了,因为我无法对字符串做任何事情,有什么想法吗?

【问题讨论】:

  • string.replace('""','"'); 你应该在服务器端 IMO 上解决这个问题!
  • 你能修改后端返回的内容吗?
  • 我可以要求 java 开发人员更改它,但是要怎么做呢?

标签: javascript php jquery escaping html-escape-characters


【解决方案1】:

你必须在服务器端转义字符串,所以 javascript 接收它已经转义了,在 PHP 的情况下你可以这样做:

$var = str_replace('"', '\"', $var);

然后在 javascript 中确保您在双引号之间阅读它,因为您为此进行了转义:

//use double quotes, as you escaped for them
jQuery("#selectionkeywords").val("<?php echo $var ?>"); 

【讨论】:

  • 我可以选择转义或非转义接收它,但两者都没有帮助,因为我不能使用任何替换方法。我没有使用 php,但我尝试使用这种方法 mykeyword.replace('"', '\"') 但是如果 mykeyword 包含单引号怎么办?这仅适用于一种情况
  • 如果后端给你jQuery("#selectionkeywords").val(""hello"");,那么你就什么都做不了,因为这甚至不是有效的javascript。它是一个空字符串,标识符hello,然后是一个空字符串,它们全部组合在一起。你想要的是后端给你jQuery(#selectionkeywords").val("\"hello\"");。您可以在 PHP 中使用 addslashes($str); 执行此操作,它使用反斜杠转义单引号和双引号。
  • @Reina 如果字符串包含单引号,我上面的相同答案也适用,因为双引号内的单引号是安全的,并且我在上面的代码中使用 PHP echo $var 周围的双引号。
【解决方案2】:

当您在服务器和客户端之间交换数据时,您希望使用 base64 编码和解码,以避免此类问题。

Java 端:

import javax.xml.bind.DatatypeConverter;

/* ... */

/* Decode input from client */
String input = DatatypeConverter.printBase64Binary(inputFromClient);

/* Encode output for client */
String output = DatatypeConverter.printBase64Binary(encodedOutput);

Javascript 方面:

// Encode data for the server
var dataForServer = Base64.encode(data);

// Decode data coming from the server
var data = Base64.decode(dataFromServer);

使用这个类

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

【讨论】:

  • 我希望我对我们使用什么有发言权
  • 你说你可以问java开发者,但你不知道是什么。给你。
猜你喜欢
  • 2012-05-28
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2011-06-13
相关资源
最近更新 更多