babsher babsher - 1 year ago 82
Javascript Question

Javascript split only once and ignore the rest

I am parsing some key value pairs that are separated by colons. The problem I am having is that in the value section there are colons that I want to ignore but the

function is picking them up anyway.


Name: my name
description: this string is not escaped: i hate these colons
date: a date

On the individual lines I tried this
line.split(/:/, 1)
but it only matched the value part of the data. Next I tried
line.split(/:/, 2)
but that gave me
['description', 'this string is not escaped']
and I need the whole string.

Thanks for the help!

awm awm
Answer Source
a = line.split(/:/);
key = a.shift();
val = a.join(':');