2012-11-29 34 views
0

我有一些JavaScript問題。我從數據庫中獲取編碼的字符串並放入它的javascript數組。字符串包含\\並且當我警告字符串長度比它有更大。我如何獲得原始字符串? 這是我的字符串如何獲得編碼的字符串原始長度包含\字符在JavaScript中,jQuery的?

var a = "[email protected][email protected]~_Gf}\\|[email protected]@ilDvi][email protected]@[email protected]`@`vIlrZznB`d^[email protected]@~bK~yT~uQxsPpmA|wL_bD|[email protected]`[email protected]@u`Erw~Am|Dp{[email protected]}[email protected]@feAziLrwFhJl}Ax`LvwHz||@[email protected]@_D~j}@[email protected]_GjfuAlj]t_`@`[email protected]{\\|AbxOj}LnxK`fN|gXtyH~cBrvElnKhaPxk`@jxB~v\\[email protected]~|[email protected]@[email protected]_QprrAcaC|[email protected]`@[email protected]|opB}[email protected]`pdAjlIx|[email protected]@l`[email protected]|y\\v{`@[email protected][email protected][email protected]]|hJxzU|~AflJvyDncJpnJdkKabBn`ToeE|[email protected]{NraHvkKnfCv}Js`@pmRx}EroV`dItwQnpQlcM~jAzrGikJdaHujMjxJs`@bxQzsEjlKmyCnrJc|FzrXu`FzhJ`eE`|LvfD`}`@[email protected]`[rzL~rwAtfDbhTn|HvvKluRvqWjiNxjHnlE|@[email protected]@oFnxRigF|nO{[email protected]@[email protected]}[email protected]{AnwZvZh{[email protected][email protected]|AfmOj|FvlYzvE|[email protected]{[email protected]@[email protected]~AhyJorBzaOu|AffTpzFnaLv_W`{[email protected]~`KvuJ`[email protected]`eI~vV`yFv`[email protected]~iHtiH`[email protected]`tYv~Vlf^[email protected]@p|Rxp[[email protected]|[email protected]`|[email protected]~cIp}MjyUzuC|{O`[email protected]|[email protected]~mS|[email protected][email protected]|uSpkOrqHldHhkHl|Kjk[[email protected]@beVh|LnrJw}@~yRdk`@hhsAf~RtwSnbRblZp|CtgZfs]radA~~Bf_[[email protected]|[email protected]\\blEluFpuFmsDn|FaoCdwCqlB~hFb|D|nMthQl_FfvFsxA|tYl|[email protected]@x]xP" 


alert(a.length) //length is 1279 

$.ajax({ 
    cache : false, 
    url : 'get_polylines.action', 
    type : 'post', 
    dataType : 'json', 
    async : false, 
    success : function(result) { 
     var polyline = result.polyline; 

     var arr = new Array(); 
     var k = 0; 
     for (var j = 0; j < polyline.length; j++) { 
      arr [k] = polyline[j]; 
      k++; 
     } 
     for(var i=0; i<arr.length; i++){ 
     alert(arr[i].length); //length is 1284 
     } 
    }, 

}); 

,當我把這個字符串輸入hiiden申請,然後獲得價值也提醒長度1284

+1

'arr [i] .length'會拋出異常。 –

+1

但是'arr'在您嘗試查找其長度時沒有看到任何內容..? –

+0

我在數組中只寫了一個值 – Lusi

回答

0

使用String.split()削減了你的陣列,而不是通過字符串循環:

var a = "[email protected][email protected]~_Gf}\\|[email protected]@ilDvi][email protected]@[email protected]`@`vIlrZznB`d^[email protected]@~bK~yT~uQxsPpmA|wL_bD|[email protected]`[email protected]@u`Erw~Am|Dp{[email protected]}[email protected]@feAziLrwFhJl}Ax`LvwHz||@[email protected]@_D~j}@[email protected]_GjfuAlj]t_`@`[email protected]{\\|AbxOj}LnxK`fN|gXtyH~cBrvElnKhaPxk`@jxB~v\\[email protected]~|[email protected]@[email protected]_QprrAcaC|[email protected]`@[email protected]|opB}[email protected]`pdAjlIx|[email protected]@l`[email protected]|y\\v{`@[email protected][email protected][email protected]]|hJxzU|~AflJvyDncJpnJdkKabBn`ToeE|[email protected]{NraHvkKnfCv}Js`@pmRx}EroV`dItwQnpQlcM~jAzrGikJdaHujMjxJs`@bxQzsEjlKmyCnrJc|FzrXu`FzhJ`eE`|LvfD`}`@[email protected]`[rzL~rwAtfDbhTn|HvvKluRvqWjiNxjHnlE|@[email protected]@oFnxRigF|nO{[email protected]@[email protected]}[email protected]{AnwZvZh{[email protected][email protected]|AfmOj|FvlYzvE|[email protected]{[email protected]@[email protected]~AhyJorBzaOu|AffTpzFnaLv_W`{[email protected]~`KvuJ`[email protected]`eI~vV`yFv`[email protected]~iHtiH`[email protected]`tYv~Vlf^[email protected]@p|Rxp[[email protected]|[email protected]`|[email protected]~cIp}MjyUzuC|{O`[email protected]|[email protected]~mS|[email protected][email protected]|uSpkOrqHldHhkHl|Kjk[[email protected]@beVh|LnrJw}@~yRdk`@hhsAf~RtwSnbRblZp|CtgZfs]radA~~Bf_[[email protected]|[email protected]\\blEluFpuFmsDn|FaoCdwCqlB~hFb|D|nMthQl_FfvFsxA|tYl|[email protected]@x]xP" 
arr = a.replace(/\\/g,"\\\\").split(""); 
console.log(arr.length); //1284 

所以用這個替換您的Ajax調用的 「成功」 位:

success : function(result) { 
    var polyline = result.polyline; 

    var arr = polyline.replace(/\\/g,"\\\\").split(""); 
    var k = arr.length; //1284 
} 

輸出不同長度的原因是因爲您的字符串中有反斜槓("\")個字符。這些通常用於轉義特殊字符,並且在從字符串構建數組之前需要轉義。
替換是用.replace(/\\/g,"\\\\")完成的,用雙打替換所有單個反斜槓("\")("\\")。這導致數組返回1284的長度,這實際上是正確的:

console.log(arr); 
==> [<...> "G", "f", "}", "\", "\", "|", "a", "A", <...>] 
//Representing: 
"Gf}\\|aA"; 
+0

感謝您的回答。 該字符串表示提供谷歌地圖API的編碼方向,並且將編碼後的方向存儲在我的數據庫中。當我從數據庫中獲取該方向並在地圖中繪製它時,它會繪製錯誤的方向,當我嘗試使用suggession(從\\替換爲\\\\)時,它也會繪製錯誤的方向,但是當繪製var a時,它會正確繪製方向 – Lusi

+0

那麼看起來你不需要'.replace(/ \\/g,「\\\\」)'位? 任何方式,如果這對你有用,請[標記爲你的回答](http://meta.stackexchange.com/a/5235/203057) – Cerbrus

+0

我替換字符串替換(/​​ \\\\/g,「 \\「)並且工作。謝謝 – Lusi

相關問題