3

显然,当您自己创建一个实际的字符串文字时,您自己会使用反斜杠转义双引号字符。

var foo = "baz\"bat";

就像您使用少数其他控制字符一样,例如换行符和反斜杠。

var bar = "baz\\bat\nmynew line and a \"quote\" ";

但是,如果您只是将现有变量包装在引号字符中,即将其提供给需要引用输入的其他系统,则会出现一些混乱。

显然,您必须转义字符串中任何潜在的双引号字符。

var doubleQuoteRe = /\"/g;
var quoted = "\"" + unquoted.replace(escaper, '\\\"') + "\"";

但是根据某些人的说法,您现在必须担心在变量中转义文字反斜杠字符。换句话说,使用比我的小正则表达式大得多的锤子。但是我不明白为什么。

4

4 回答 4

2

您可能希望避免转义已经转义的引号 -

String.prototype.inquotes=function(){
 return '"'+this.replace(/(^|[^\\])"/g,'$1\\"')+'"';
}
于 2010-03-08T22:20:53.207 回答
1

答案是肯定的,你必须做两件事:

  1. 用两个反斜杠替换字符串中的文字反斜杠字符,
  2. 然后,您继续将任何出现的“替换为\”。

为什么第 1 步必不可少,最简单的解释是考虑 5 个字符的字符串:

foo\"   

在前 3 个字符 (foo) 之后,字符串中有一个文字反斜杠字符,然后是一个文字双引号字符。

(换句话说,作为字符串文字,这看起来像“foo\”“)

如果我只替换引号字符,我最终会得到一个带引号的字符串,其值为

foo\\"     

但是这里的两个反斜杠将被解释为一个反斜杠。所以当我把这个值用引号括起来时,我会得到不平衡的引号。

"foo\\""

另一方面,如果我先执行第 1 步——用双反斜杠替换所有反斜杠会给出

foo\\"

然后第 2 步——用 slash-quote 替换引号给出

foo\\\"

现在,当我用引号字符包装我的值时,我终于得到了

"foo\\\""

哪个是对的。

于 2010-03-08T21:46:01.190 回答
1

FF 中有一个非标准的 str.quote()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/quote 他们建议使用以下 polyfill

    if(!String.prototype.quote){
  // oop version - no dependencies
  String.prototype.quote = (function(){
    // prepare fallback
    // ----------------
    // backslash escape double quotes and backslashes
    var escp_regex = /[\\"]/g,
      escp_callback = '\\$&',
      // escape control characters
      ctrl_map = {
        '\b': '\\b', // backspace
        '\t': '\\t', // tab
        '\n': '\\n', // new line
        '\f': '\\f', // form feed
        '\r': '\\r'  // carriage return
      },
      // don't rely on `Object.keys(ctrl_map).join('')`
      ctrl_regex = new RegExp('[\b\t\n\f\r]', 'g'),
      ctrl_callback = function(match){
        return ctrl_map[match];
      },
      // hex-escape, spare out control characters and ASCII printables
      // [0-7,11,14-31,127-255]
      xhex_regex = /[\x00-\x07\x0B\x0E-\x1F\x7F-\xFF]/g,
      xhex_callback = function(match, char_code){
        char_code = match.charCodeAt(0);
        return '\\x' + (char_code < 16 ? '0' : '') + char_code;
      },
      // hex-escape all others
      uhex_regex = /[\u0100-\uFFFF]/g,
      uhex_callback = function(match, char_code){
        char_code = match.charCodeAt(0);
        return '\\u' + (char_code < 4096 ? '0' : '') + char_code;
      },
      // delegate to native `JSON.stringify` if available
      stringify = typeof JSON !== 'undefined' && JSON.stringify;

    // return actual polyfill
    // ----------------------
    return function(){
      var self = this; // promote compression
      if(self == null) throw new TypeError('can\'t convert ' + self + ' to object');
      if(stringify) return stringify(self);
      return '"' + self
        .replace(escp_regex, escp_callback)
        .replace(ctrl_regex, ctrl_callback)
        .replace(xhex_regex, xhex_callback)
        .replace(uhex_regex, uhex_callback) + '"';
    }
  }());

  // generic version - requires Function#bind
  String.quote = Function.call.bind(''.quote);
}
于 2014-09-30T17:30:22.683 回答
0

您可能希望转义除引号之外的其他字符,例如空格字符(换行符!)和/或非 ASCII 字符。有Crockford 的quote(),我自己的实现可以在mercurial.intuxication.org找到。

于 2010-03-08T18:21:03.483 回答