 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to split comma and semicolon separated string into a two-dimensional array in JavaScript ?
Let's say we have a variable “users” that contains the following string of text where each user is separated by a semicolon and each attribute of each users is separated by a comma −
const users = 'Bob,1234,Bob@example.com;Mark,5678,Mark@example.com';
We are required to write a JavaScript function that takes in one such string and splits this into a multidimensional array that looks like this −
const arr = [ ['Bob', 1234, 'Bob@example.com'], ['Mark', 5678, 'Mark@example.com'] ];
Example
The code for this will be −
const users = 'Bob,1234,Bob@example.com;Mark,5678,Mark@example.com'; const splitByPunctuations = (str = '') => {    let res = [];    res = str.split(';');    for(let i = 0; i < res.length; i++){       res[i] = res[i].split(',');    };    return res; }; console.log(splitByPunctuations(users));  Output
And the output in the console will be: [ [ 'Bob', '1234', 'Bob@example.com' ], [ 'Mark', '5678', 'Mark@example.com' ] ]
Advertisements
 