Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将特殊字符串拆分为数组_Javascript_Arrays_Split - Fatal编程技术网

Javascript 将特殊字符串拆分为数组

Javascript 将特殊字符串拆分为数组,javascript,arrays,split,Javascript,Arrays,Split,我有这根绳子 var string = 'foo , bar , "special1,special2,special3" , "random1,random2" , normal , another item' 我想在,上拆分该字符串,并将其保留在引号内,就像在数组中一样,也就是说,我不想在引号内拆分字符串。因此,预期的产出将是 var array = ['foo', 'bar', 'special1,special2,special3', 'random1,random2', 'norma

我有这根绳子

var string = 'foo , bar , "special1,special2,special3" , "random1,random2" , normal , another item'
我想在
上拆分该字符串,并将其保留在引号内,就像在数组中一样,也就是说,我不想在引号内拆分字符串。因此,预期的产出将是

var array = ['foo', 'bar', 'special1,special2,special3', 'random1,random2', 'normal', 'another item']

您可以使用
分隔符拆分字符串,然后从数组的任何元素中删除双引号

var string='foo,bar,“special1,special2,special3”,“random1,random2”,normal,other item';
var array=string.split(',').map(函数(x){
返回x.replace(/“/g,”)
});

警报(JSON.stringify(array));
谢谢。编辑string@Pointy我尝试使用
split(',')进行拆分
。当然不起作用。我尝试了循环来检测引号。这也没有给我结果。为什么不投票?这不起作用。它也会将引号内的内容拆分。为什么不投票?检查更新的答案。@void you's late:)。尽管我的投票结果是向上的:D@PardeepDhingra是的,但我是正确的:)我已经删除了你的ES6片of代码,因为这个问题并没有专门询问ES6,而且还不是每个浏览器都支持ES6。