本文介绍了将字符串拆分为键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果与 key:value
相匹配,我想用逗号分割以下字符串.按逗号分隔可起作用,直到在 value
I want to split the following string by comma if it matches key: value
. Split by comma works until it encounters a comma in the value
const string =国家:肯尼亚,城市:内罗毕,人口:3.375M,民主描述:工作进行中/未完全解决,存在障碍"
我想得出以下结果:
[[country: Kenya],
[city: Nairobi],
[population: 3.375M],
[democracy-description: Work in progress/ Not fully met, obstacles exist]]
谢谢.
推荐答案
您可以通过查看是否没有逗号和冒号来拆分字符串.
You could split the string by looking if not a comma follows and a colon.
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; }
这篇关于将字符串拆分为键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!