javascript - split string into key value pair -


i want split following string comma if matches key: value. split comma works until encounters comma in value

const string = "country: kenya, city: nairobi, population: 3.375m, democracy-desciption: work in progress/ not met, obstacles exist"

i'd end result:

[[country: kenya], [city: nairobi], [population: 3.375m], [democracy-description: work in progress/ not met, obstacles exist]] 

thank in advance.

you split string looking if not comma follows , colon.

var string = "country: kenya, city: nairobi, population: 3.375m, democracy-desciption: work in progress/ not met, obstacles exist, foo: bar, bar, bar";    console.log(string.split(/, (?=[^,]+:)/).map(s => s.split(': ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -