Nishant123
Nishant123

Reputation: 1966

Regex for creating an array from String

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?

Upvotes: 2

Views: 64

Answers (4)

Pranav C Balan
Pranav C Balan

Reputation: 115212

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);

Upvotes: 4

epascarello
epascarello

Reputation: 207501

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]);

Upvotes: 0

Redu
Redu

Reputation: 26161

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);

Upvotes: 0

Tushar
Tushar

Reputation: 87203

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.

Upvotes: 1

Related Questions