【问题标题】:How do I add slashes to a string in Javascript?如何在 Javascript 中为字符串添加斜杠?
【发布时间】:2011-01-12 19:50:05
【问题描述】:

只是一个字符串。每次有单引号时添加 \'。

【问题讨论】:

  • 这最好不要用于防止sql注入,因为攻击者可以轻松绕过它。漏洞利用代码不必执行您的 javascript,它只会发送请求。转义和清理输入应始终在服务器端完成。
  • 或者,您可能只是想为自己节省一些 PHP,并按照问题的具体要求在客户端执行此操作。
  • Rook 的意思是您不应该为自己节省一些 PHP 并在客户端执行此操作,因为攻击者可以轻松绕过任何客户端代码。跨度>

标签: javascript jquery formatting string-formatting backslash


【解决方案1】:
var myNewString = myOldString.replace(/'/g, "\\'");

【讨论】:

    【解决方案2】:

    replace 适用于第一个引号,因此您需要一个很小的正则表达式:

    str = str.replace(/'/g, "\\'");
    

    【讨论】:

    • @AlexeyLebedev - 第二个参数不是正则表达式模式 - 您只需要在字符串中使用一个反斜杠。
    • 让我们看看:str = "\\'"; str = str.replace(/'/g, "\\'");
    • 产生一个带有转义斜杠和 未转义 引号的字符串。虽然您的示例是该问题的正确答案,但它不能正确转义字符串。
    • @Alexey - Haaa...这就是你的意思。那你是对的。如果需要,它很容易修复。
    • 亲爱的匿名投票者 - 我只能假设我没有满足只存在于你脑海中的要求。
    【解决方案3】:
    var str = "This is a single quote: ' and so is this: '";
    console.log(str);
    
    var replaced = str.replace(/'/g, "\\'");
    console.log(replaced);
    

    给你:

    This is a single quote: ' and so is this: '
    This is a single quote: \' and so is this: \'
    

    【讨论】:

      【解决方案4】:

      可以肯定的是,您不仅需要替换单引号,还需要替换已经转义的引号:

      "first ' and \' second".replace(/'|\\'/g, "\\'")
      

      【讨论】:

        【解决方案5】:

        如果您正在执行替换以准备将字符串发送到 alert() 或其他任何单引号字符可能会绊倒您的地方,那么您没有要求的答案可能会有所帮助。

        str.replace("'",'\x27')
        

        这会将所有单引号替换为十六进制代码作为单引号。

        【讨论】:

        • 这些字符串是相同的。除非你的意思是 '\\x27'。
        【解决方案6】:

        以下 JavaScript 函数处理 '、"、\b、\t、\n、\f 或 \r 等价于 php 函数 addlashes()。

        function addslashes(string) {
            return string.replace(/\\/g, '\\\\').
                replace(/\u0008/g, '\\b').
                replace(/\t/g, '\\t').
                replace(/\n/g, '\\n').
                replace(/\f/g, '\\f').
                replace(/\r/g, '\\r').
                replace(/'/g, '\\\'').
                replace(/"/g, '\\"');
        }
        

        【讨论】:

        • 这个函数会做相反的事情,希望可以在这里发布......哎呀.. String.prototype.stripSlashes = function(){ return this.replace(/\(.)/mg, "$1"); }
        【解决方案7】:

        使用 JSON.stringify 可以全面而紧凑地转义字符串。从 ECMAScript 5 开始,它是 JavaScript 的一部分,并受到主要较新浏览器版本的支持。

        str = JSON.stringify(String(str));
        str = str.substring(1, str.length-1);
        

        使用这种方法,特殊字符作为空字节、unicode 字符和换行符\r\n 也可以在相对紧凑的语句中正确转义。

        【讨论】:

        • 受全球使用份额大于 0.1% 的所有浏览器支持,但 IE 版本 8 必须处于标准模式。
        【解决方案8】:
        if (!String.prototype.hasOwnProperty('addSlashes')) {
            String.prototype.addSlashes = function() {
                return this.replace(/&/g, '&') /* This MUST be the 1st replacement. */
                     .replace(/'/g, ''') /* The 4 other predefined entities, required. */
                     .replace(/"/g, '"')
                     .replace(/\\/g, '\\\\')
                     .replace(/</g, '&lt;')
                     .replace(/>/g, '&gt;').replace(/\u0000/g, '\\0');
                }
        }
        

        用法:alert(str.addSlashes());

        参考:https://stackoverflow.com/a/9756789/3584667

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-18
          • 2013-05-29
          • 2011-01-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多