How to split a string and breaking it to an array?
I have an string as shown below
1234,1243,"555,552,553"
Using JavaScript, what is the fastest way to split the string and achieve below array
['1234','1243','555,552,553']
How to split a string and breaking it to an array?
I have an string as shown below
1234,1243,"555,552,553"
Using JavaScript, what is the fastest way to split the string and achieve below array
['1234','1243','555,552,553']
You need to use a regex i thinks, maybe there are better way, but it's need to iterate multiple time.
const str = '1234,1243,"555,552,553"';
const regex = /"[^"]+"|[^,]+/g;
const result = str.match(regex).map((item) => item.replace(/"/g, ''));
The result is -> array(3) ['1234', '1243', '555,552,553']