如何得到编码的字符串原始长度在javascript,jquery中包含\\字符?

时间:2012-11-29 08:10:11

标签: javascript jquery

我在使用JavaScript时遇到了一些问题。我从数据库中获取编码字符串并放入其javascript数组。 string包含\\,当我警告字符串长度大于它时。我怎么能得到原始字符串? 这是我的字符串

var a = "eir~FdezuOha@ha_@rPzjQbnChxI~_Gf}\\|aAfnu@exNjmm@ilDvi]~Ttnz@tmLlyg@vi@nj`@`vIlrZznB`d^dd@zsVbvFdia@~bK~yT~uQxsPpmA|wL_bD|cCwiEfaEil@d`s@keEzri@u`Erw~Am|Dp{x@qJjbfBx}AdyrDkPtme@tgClei@feAziLrwFhJl}Ax`LvwHz||@nmHtrp@hGnnc@_D~j}@a@hskA_GjfuAlj]t_`@`sTheWjs@h{\\|AbxOj}LnxK`fN|gXtyH~cBrvElnKhaPxk`@jxB~v\\tqK~lLuUvkh@kBz~|As@tyfCfCbck@dbJhfh@h_QprrAcaC|czAunHbsr@qkMpm`@osk@|opB}nPvjvAthDp_kBhQdvt@zyK`pdAjlIx|OdiJln_@ngKzzu@l`YtdeAlqQrjZfzc@|y\\v{`@zks@h_@xcWf_CjoaArzNrbkAbfNb~c@nsVzeYjlTno]|hJxzU|~AflJvyDncJpnJdkKabBn`ToeE|pYcr@thRqpAh{NraHvkKnfCv}Js`@pmRx}EroV`dItwQnpQlcM~jAzrGikJdaHujMjxJs`@bxQzsEjlKmyCnrJc|FzrXu`FzhJ`eE`|LvfD`}`@rrDrr_@diHbdQbaBzrRajAd`[rzL~rwAtfDbhTn|HvvKluRvqWjiNxjHnlE|@ppKpfJbtDroDth@vre@oFnxRigF|nO{bEbwRwpGhmLld@rrKxuCbbRbnKbhSnve@vrr@}sBxqs@f{AnwZvZh{a@qnGdnSa~AlrKfmLjhe@q|AfmOj|FvlYzvE|br@j{Axr_@hkFfre@jhIdrg@udJbuRknHpbBgzBpdFc~AhyJorBzaOu|AffTpzFnaLv_W`{d@lhPrfJvxEfnJ~`KvuJ`nE~bTgzGztVwf@vsHngJ`eI~vV`yFv`m@sgAhhGwcC~iHtiH`ed@ruUbtO`tYv~Vlf^hdHlq@bnFvqLxin@p|Rxp[rkWnyTzka@jtVfqLvtF|aa@vcF`|Erc@~cIp}MjyUzuC|{O`lGp~Qhn@|rc@~mS|eg@vtZzi_@hqL|uSpkOrqHldHhkHl|Kjk[bgLdxItqa@jtDjiPbbDhwQryQlou@beVh|LnrJw}@~yRdk`@hhsAf~RtwSnbRblZp|CtgZfs]radA~~Bf_[zvArjTrcFfjNtzb@xgWziUtsN|af@tp\\blEluFpuFmsDn|FaoCdwCqlB~hFb|D|nMthQl_FfvFsxA|tYl|AnwP_Fzca@zbApjr@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 个答案:

答案 0 :(得分:0)

使用String.split()来剪切数组,而不是循环遍历字符串:

var a = "eir~FdezuOha@ha_@rPzjQbnChxI~_Gf}\\|aAfnu@exNjmm@ilDvi]~Ttnz@tmLlyg@vi@nj`@`vIlrZznB`d^dd@zsVbvFdia@~bK~yT~uQxsPpmA|wL_bD|cCwiEfaEil@d`s@keEzri@u`Erw~Am|Dp{x@qJjbfBx}AdyrDkPtme@tgClei@feAziLrwFhJl}Ax`LvwHz||@nmHtrp@hGnnc@_D~j}@a@hskA_GjfuAlj]t_`@`sTheWjs@h{\\|AbxOj}LnxK`fN|gXtyH~cBrvElnKhaPxk`@jxB~v\\tqK~lLuUvkh@kBz~|As@tyfCfCbck@dbJhfh@h_QprrAcaC|czAunHbsr@qkMpm`@osk@|opB}nPvjvAthDp_kBhQdvt@zyK`pdAjlIx|OdiJln_@ngKzzu@l`YtdeAlqQrjZfzc@|y\\v{`@zks@h_@xcWf_CjoaArzNrbkAbfNb~c@nsVzeYjlTno]|hJxzU|~AflJvyDncJpnJdkKabBn`ToeE|pYcr@thRqpAh{NraHvkKnfCv}Js`@pmRx}EroV`dItwQnpQlcM~jAzrGikJdaHujMjxJs`@bxQzsEjlKmyCnrJc|FzrXu`FzhJ`eE`|LvfD`}`@rrDrr_@diHbdQbaBzrRajAd`[rzL~rwAtfDbhTn|HvvKluRvqWjiNxjHnlE|@ppKpfJbtDroDth@vre@oFnxRigF|nO{bEbwRwpGhmLld@rrKxuCbbRbnKbhSnve@vrr@}sBxqs@f{AnwZvZh{a@qnGdnSa~AlrKfmLjhe@q|AfmOj|FvlYzvE|br@j{Axr_@hkFfre@jhIdrg@udJbuRknHpbBgzBpdFc~AhyJorBzaOu|AffTpzFnaLv_W`{d@lhPrfJvxEfnJ~`KvuJ`nE~bTgzGztVwf@vsHngJ`eI~vV`yFv`m@sgAhhGwcC~iHtiH`ed@ruUbtO`tYv~Vlf^hdHlq@bnFvqLxin@p|Rxp[rkWnyTzka@jtVfqLvtF|aa@vcF`|Erc@~cIp}MjyUzuC|{O`lGp~Qhn@|rc@~mS|eg@vtZzi_@hqL|uSpkOrqHldHhkHl|Kjk[bgLdxItqa@jtDjiPbbDhwQryQlou@beVh|LnrJw}@~yRdk`@hhsAf~RtwSnbRblZp|CtgZfs]radA~~Bf_[zvArjTrcFfjNtzb@xgWziUtsN|af@tp\\blEluFpuFmsDn|FaoCdwCqlB~hFb|D|nMthQl_FfvFsxA|tYl|AnwP_Fzca@zbApjr@x]xP"
arr = a.replace(/\\/g,"\\\\").split("");
console.log(arr.length); //1284

所以用这个替换你的ajax调用的“success”位:

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";