js字符串转数组

请问,以下字符串如何拆分成数组,要求不能拆数组和对象:

const text = "a,b,c,[1,2,3],d,{a:1,b:2}";


const parseText = (text) => {
    //todo
}

parseText(text); //["a","b","c","[1,2,3]","d","{a:1,b:2}"];
阅读 1.8k
2 个回答
// =>  ["a", "b", "c", "[1,2,3]", "d", "张三", "{a:1,b:2}", "hello"]
'a,b,c,[1,2,3],d,张三,{a:1,b:2},hello,'.match(/((\[.+\])|{.+}|([^,]+))/g)
const parseText = (text) => {
    return text.match(/((\[.+\])|{.+}|([^,]+?))/g)
}

parseText('a,b,c,[1,2,3],d,{a:1,b:2}');
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题