gpt4 book ai didi

javascript - 将字符串拆分为键值对

转载 作者:行者123 更新时间:2023-11-30 08:25:39 28 4
gpt4 key购买 nike

如果匹配 key: value,我想用逗号分隔下面的字符串。按逗号拆分一直有效,直到它在 value

中遇到逗号

const string = "country: Kenya, city: Nairobi, population: 3.375M, democracy-desciption: Work in progress/未完全满足,存在障碍"

我想以这样的结果结束:

[[country: Kenya],
[city: Nairobi],
[population: 3.375M],
[democracy-description: Work in progress/ Not fully met, obstacles exist]]

提前谢谢你。

最佳答案

您可以通过查看后面是否有逗号和冒号来拆分字符串。

var string = "country: Kenya, city: Nairobi, population: 3.375M, democracy-desciption: Work in progress/ Not fully met, obstacles exist, foo: bar, bar, bar";

console.log(string.split(/, (?=[^,]+:)/).map(s => s.split(': ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

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