2

I have a string as follows

var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";

I want to get three arrays from above string as follows

var arr1 = ["series-3","series-5","series-6"];

var arr2 = ["a3","a4","a5"];

var arr3 = ["class a", "class b"];

What regex should I use to achieve this? Can this be done without regex?

1
  • You don't need regex to do that. Commented Jun 8, 2016 at 12:29

4 Answers 4

4

Use String#split() method

var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";

// split string based on comma followed by [
var temp = str.split(/,(?=\[)/);

// remove [ and ] from string usning slice
// then split using , to get the result array
var arr1 = temp[0].slice(1, -1).split(',');
var arr2 = temp[1].slice(1, -1).split(',');
var arr3 = temp[2].slice(1, -1).split(',');

console.log(arr1, arr2, arr3);


Or same method with some variation

var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";

// Remove [ at start and ] at end using slice
// and then split string based on `],[`
var temp = str.slice(1, -1).split('],[');

// then split using , to get the result array
var arr1 = temp[0].split(',');
var arr2 = temp[1].split(',');
var arr3 = temp[2].split(',');

console.log(arr1, arr2, arr3);

Sign up to request clarification or add additional context in comments.

Comments

1

RegEx and String methods can be used. It's better to create an object and store individual arrays inside that object.

var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";

// Match anything that is inside the `[` and `]`
var stringsArr = str.match(/\[[^[\]]*\]/g);

// Result object
var result = {};

// Iterate over strings inside `[` and `]` and split by the `,`
stringsArr.forEach(function(str, i) {
    result['array' + (i + 1)] = str.substr(1, str.length - 2).split(',');
});

console.log(result);

var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";
var stringsArr = str.match(/\[[^[\]]*\]/g);

var result = {};
stringsArr.forEach(function(str, i) {
    result['array' + (i + 1)] = str.substr(1, str.length - 2).split(',');
});

console.log(result);

To create the global variables(Not recommended), just remove var result = {}; and replace result by window in the forEach.

Comments

0

I would prefer to do it like this

var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]",
   arrs = str.match(/[^[]+(?=])/g).map(s => s.split(","));
console.log(arrs);

Comments

0

Just for the fun of it, another way where we add the missing quotes and use JSON.parse to convert it to a multidimensional array.

var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";
var result = JSON.parse("[" + str.replace(/\[/g,'["').replace(/\]/g,'"]').replace(/([^\]]),/g,'$1","') + "]");

console.log(result[0]);
console.log(result[1]);
console.log(result[2]);

1 Comment

You can simplify the replace calls to: str.replace(/[^[,\]]+/g, '"$&"')

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.