Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

// => ?["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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...