如何使用jquery拆分此字符串?

时间:2014-03-26 05:07:54

标签: javascript jquery

如何使用jquery分割此字符串?

"[10.072721346470422,76.32974624633789][[10.075854059674523,76.32043361663818],[10.073650930297095,76.32888793945312],[10.074918540288232,76.33090496063231],[10.073862198974942,76.33137702941895],[10.071960775892242,76.33034706115723],[10.070819916662579,76.32674217224121]]"

答案

第一个数组元素:

  

[10.072721346470422,76.32974624633789]

第二个数组元素:

  

[[10.075854059674523,76.32043361663818],[10.073650930297095,76.32888793945312],[10.074918540288232,76.33090496063231],[10.073862198974942,76.33137702941895]   [10.071960775892242,76.33034706115723],[10.070819916662579,76.32674217224121]]

1 个答案:

答案 0 :(得分:1)

你可以这样做:

var s = "[10.072721346470422,76.32974624633789][[10.075854059674523,76.32043361663818],[10.073650930297095,76.32888793945312],[10.074918540288232,76.33090496063231],[10.073862198974942,76.33137702941895],[10.071960775892242,76.33034706115723],[10.070819916662579,76.32674217224121]]";
var arr = s.split('][');
arr[0] = arr[0] + ']';
arr[1] = "[" + arr[1];

如果你想要数组,请执行以下操作:

var firstArr=arr[0].replace(/[\[\]]/g,"").split(',');
var secondArr=arr[1].replace(/[\[\]]/g,"").split(',');

演示:http://jsfiddle.net/GCNaK/