js split string into array by comma and space
JavaScript[Edit]
+
0
-
0
js split string into array by comma and space
1myString.split(/[ ,]+/);
[Edit]
+
0
-
0
js split string into array by comma and space
1 2 3 4 5 6 7 8 9 10const text1 = 'Split,this text'; const text2 = 'Another, , ,example,,,,text to , , , split'; // split strings const result1 = text1.split(/[ ,]+/); const result2 = text2.split(/[ ,]+/); // results: console.log(result1); // ['Split', 'this', 'text'] console.log(result2); // ['Another', 'example', 'text', 'to', 'split']
[Edit]
+
0
-
0
js split string into array by comma and space
1 2 3 4 5 6 7 8 9 10 11 12 13function splitString(string) { return string.split(/[ ,]+/); } // Usage example: const text1 = 'Split,this text'; const text2 = 'Another, , ,example,,,,text to , , , split'; console.log( splitString(text1) ); // ['Split', 'this', 'text'] console.log( splitString(text2) ); // ['Another', 'example', 'text', 'to', 'split']