2010-03-08 46 views
3

显然,当你自己创建一个实际的字符串文字时,你自己使用双引号字符进行反斜杠转义。如何在双引号字符中安全地包装JS字符串变量?

var foo = "baz\"bat"; 

正如你对少数其他控制字符,如换行符和反斜杠一样。

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

,但如果你只是包裹在引号字符,现有的变量,即把它送给需要引用输入一些其他的系统,有一些混乱。

显然你必须转义字符串中任何可能的双引号字符。

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

但据一些你现在担心的变量转义反斜杠字符。换句话说,使用比我的小正则表达式更大的锤子。但是我不明白为什么。

+0

为什么不能这样工作? 'var s1 =“a \”b“; var s2 =”\“”+ s1 +“\”“;'? – 2010-03-08 18:13:40

+2

当你说”你已经拥有一个变量“时,你是什么意思?你有什么变数,你需要用它来做什么?这个问题对我来说没有意义。 – Pointy 2010-03-08 18:14:28

+0

谢谢 - 是的,我最后的方式让我感到困惑,我更新了这个问题。 – nmealy 2010-03-08 20:04:34

回答

0

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

  1. 替换反斜杠字符的字符串中,以两个反斜杠,
  2. 然后,在继续更换任何出现“与\” 。

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

foo\" 

的前3个字符(富)之后,有在字符串中反斜线字符,然后有一个字面的双引号字符。

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

如果我只能更换引号字符,我会带引号的字符串结束,它的值是

foo\\"  

但是,这里的两个反斜杠将被解释为单个反斜杠,所以当我将这个值包含在引号中时,最终会导致不平衡的引号。

"foo\\"" 
,另一方面

,如果我做的第1步先 - 双反斜杠替换所有反斜杠给

foo\\" 

,然后第2步 - 更换用斜线引号的报价给

现在
foo\\\" 

,当我在引号字符缠上了我的价值,我终于得到

"foo\\\"" 

这是正确的。

2

你可能想避免逃避报价你已经escaped-

String.prototype.inquotes=function(){ 
return '"'+this.replace(/(^|[^\\])"/g,'$1\\"')+'"'; 
} 
1

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/quote 他们提出以下填充工具

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); 
} 
相关问题