2010-03-08 62 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); 
}