1

I have a string like this

var str = "error,#FFFFFF,won,25,[{\"color\":\"#7ac1de\",\"from\":\"10\",\"to\":\"60\"}]"

I will be splitting the string based on comma (',') in the string. But i need to split the string with out affecting comma inside [{\"color\":\"#7ac1de\",\"from\":\"10\",\"to\":\"60\"}].

So my expected output will be ['error', '#FFFFFF', 'won', '25', '[{"color":"#7ac1de","from":"10","to":"60"}]'] .
I tried with many ways but not working. Please help me.

htoniv
  • 1,658
  • 21
  • 40

2 Answers2

3

Something like this ?

var str = "error,#FFFFFF,won,25,[{\"color\":\"#7ac1de\",\"from\":\"10\",\"to\":\"60\"}]";

var split = str.split(/,(?![^{]*})/);

console.log(split);

Regex source

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
0

This will provide your expected output: (checked)

var str = "error,#FFFFFF,won,25,[{\"color\":\"#7ac1de\",\"from\":\"10\",\"to\":\"60\"}]";

var res = str.split(",");