0

I get a string from an end point, the string will be in any of the forms as shown below

Input:

First Form

Option1[opt1,opt4,opt8,opt7],Option5[opt2,opt3],Option6[opt5,opt6] 

Expected output:

[ 
  Option1[opt1,opt4,opt8,opt7],
  Option5[opt2,opt3], 
  Option6[opt5,opt6]
]

or

Option1[1,4,8,7],Option5[2,3],Option6[5,6]  

Expected output:

[ 
  Option1[1,4,8,7],
  Option5[2,3], 
  Option6[5,6]
] 

Second Form

Option1[opt1],Option5[opt2],Option6[opt6]

Expected output:

[ 
  Option1[opt1],
  Option5[opt2], 
  Option6[opt5]
]

or

Option1[1],Option5[2],Option6[6]

Expected output:

[ 
  Option1[1],
  Option5[2], 
  Option6[5]
]

Third Form

Option1,Option5,Option6 or Item1,Item2,Item3 or abc1,xyz2,ccc3

Expected output

[ 
  Option1,
  Option5, 
  Option6
]

for the Second and Third forms

.split(',') 

works and returns expected array but here i am facing challenge with First form, looking for some options (reg exp) that can consider , after ].

Note: Sorry for making changes in the question as i see input coming from endpoint is changing, but now i will not make any more edits for the inputs in the question

3
  • 3
    Please explain the expected output, it is not valid JS. Commented Mar 28, 2017 at 4:10
  • stackoverflow.com/questions/25988636/… Commented Mar 28, 2017 at 4:14
  • Your "expected output", aside from not being valid JS, doesn't seem to cover the third input format. Commented Mar 28, 2017 at 4:22

2 Answers 2

2

Split comma "," followed by upper case "O"

str.split(/,(?=O)/)
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks @guest271314 that worked if string contains Optionsxx what if it has different value like my updated question for Third form Option1,Option5,Option6 or Item1,Item2,Item3 or abc1,xyz2,ccc3
@Sri If letter character begins matching pattern you can use RegExp /,(?=[a-z])/i to match comma , followed by a-z case insensitive var str = "Option1[1,4,8,7],Option5[2,3],Option6[5,6],abc1,xyz2,ccc3,Item1,Item2,Item3"; var res = str.split(/,(?=[a-z])/i); console.log(res);
I just noticed that the input is having another form which has strings with in the array i.e Option1[opt1,opt4,opt8,opt7],Option5[opt2,opt3],Option6[opt5,opt6] in that case it is almost behaving like .split(',')
You have now changed requirement twice. See stackoverflow.com/help/how-to-ask
i mean if the given string is Option1[opt1,opt4,opt8,opt7],Option5[opt2,opt3],Option6[opt5‌​,opt6] i am getting the output as ["Option1[opt1","opt4","opt8","opt7]","Option5[opt2","opt3]","Option6[opt5‌​","opt6]"] for your suggested answer. The length of the result array is supposed to be 3 but it is 8
|
0

Try with below, using regex:

var check1 = 'Option1[1,4,8,7],Option5[2,3],Option6[5,6]';
var check2 = 'Option1[1],Option5[2],Option6[6]';
var check3 = 'Item1,Item2,Item3';

var regex = /\s*,\s*(?![^\[\]]*\])/g;

var arr1 = check1.split(regex);
var arr2 = check2.split(regex);
var arr3 = check3.split(regex);

console.log(arr1);
console.log(arr2);
console.log(arr3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.