babsher babsher - 3 months ago 17
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

split
function is picking them up anyway.

sample:

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
a = line.split(/:/);
key = a.shift();
val = a.join(':');
Comments