Alex Rivera | Logout

Javascript split only once and ignore the rest

Asked 2011-04-21T16:12:49.250
13

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!

Edit
Report

1 Answer

15

Use the greedy operator (?) to only split the first instance.

line.split(/: (.+)?/, 2);

answered 2011-04-21T16:18:36.557

Your Answer