Todd Miller
Todd Miller

Reputation: 199

Split string into array that has brackets in string

I have a string in this format:

"[0.00,0.10],[3.00,0.10],[6.00,0.10],[9.00,0.10],[12.00,662.00],[15.00,1186.00]"

I want to split it into an array so that the final array would look like this. Basically they are [x,y] pairs to use in a highcharts chart.

[[0.00,0.10], [3.00,0.10], [6.00,0.10], [9.00,0.10], [12.00,662.00], [15.00,1186.00]]

Using javascript how is this possible?

I have tried using regular expression, but cannot seem to get it to work.

Upvotes: 2

Views: 120

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386570

You could add brackets and parse as JSON with JSON.parse.

var string =  "[0.00,0.10],[3.00,0.10],[6.00,0.10],[9.00,0.10],[12.00,662.00],[15.00,1186.00]",
    array = JSON.parse('[' + string + ']');
    
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 12

Related Questions