gpt4 book ai didi

javascript - 将字符串拆分为多组字符串

转载 作者:行者123 更新时间:2023-11-30 09:05:15 25 4
gpt4 key购买 nike

我有一个包含元素列表的字符串,其中一些是固定的,一些是可选取的。例如,假设我的字符串是购物 list :

"strawberry, raspberry, $2{tomato, sauerkraut, beans},
potato, $1{{apple, pear}, {orange, banana}}"

我想以这样的方式结束:

["strawberry",
"raspberry",
[2, "tomato", "sauerkraut", "beans"],
"potato",
[1, ["apple", "pear"], ["orange", "banana"]]

我不知道如何处理这里的多项选择,所以我愿意接受所有建议。您也可以修改“语法”(例如 "$[num of coices]{list}")并添加/删除绒毛,这并不重要。越简单越好! :]

编辑:我有无数个上述格式的列表,除非没有其他解决方案,否则无法手动转换它们...:(

谢谢!

最佳答案

你为什么不使用 JSON为此?

var list = ["strawberry",
"raspberry",
[2, "tomato", "sauerkraut", "beans"],
"potato",
[1, ["apple", "pear"], ["orange", "banana"]]];

var str = JSON.stringify(list);
/*
str = '["strawberry","raspberry",[2,"tomato","sauerkraut","beans"],"potato",[1,["apple","pear"],["orange","banana"]]]'
*/

var list2 = JSON.parse(str);
/*
list2 = ["strawberry",
"raspberry",
[2,"tomato","sauerkraut","beans"],
"potato",
[1, ["apple","pear"], ["orange","banana"]]]
*/

关于javascript - 将字符串拆分为多组字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5566569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com