从字符串创建2d数组

时间:2011-02-12 10:14:26

标签: javascript arrays string multidimensional-array

我有以下字符串:

[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],]

如何从中创建二维数组字符串?


修改

我删除了html标签,因为它们不是问题所在。此外,我想在不使用任何其他库的情况下实现它,以保持轻量级。

2 个答案:

答案 0 :(得分:2)

除了其中的HTML标记,它将是有效的JSON。您可以删除HTML标记并使用任何处理JSON的库来解析它,例如jQuery:

var arr = $.parseJSON(theString.replace(/<br\/>/g,''));

它也是删除了HTML标记的有效Javascript代码,因此如果您完全控制字符串的来源,以确保它永远不会包含任何有害代码,则可以使用{{1}函数执行字符串:

eval

答案 1 :(得分:-1)

您需要从字符串中删除<br/>。然后你应该能够做到:

var my2darray = eval(mystring);