How to break a regular season such a string with quotes inside?

There are lines like this:
'aaaaaa;bbbbb;;;ccccc;"dddd;eeee";ffff;"gggg;;hhhh";jjjj'

Should be able to:
[
 "aaaaaa", 
 "bbbbb", 
 "", 
 "", 
 "ccccc", 
 "dddd;eeee", 
 "ffff", 
 "gggg;;hhhh", 
"jjjj",
]

Banal .split(";") is not suitable.
March 12th 20 at 07:57
2 answers
March 12th 20 at 07:59
Solution
The regular season will not work.
function parse(input) {
 const result = []
 let i = 0;
 while (true) {
 let j
 if (input[i] === '"') {
 j = input.indexOf('"', i + 1) + 1
 } else {
 j = input.indexOf(';', i)
}
 if (j === -1) {
result.push(input.slice(i))
break
}
 result.push(input.slice(i, j))
 i = j + 1
}
 return result.map(s => s[0] === '"' ? s.slice(1, -1) : s)
}
const input = 'aaaaaa;bbbbb;;;ccccc;"dddd;eeee";ffff;"gggg;;hhhh";jjjj'
console.log(parse(input))
Almost correct) - bell_Kovac commented on March 12th 20 at 08:02
@bell_Kovachas already been corrected, invisible - Velda_Durg commented on March 12th 20 at 08:05
Yes, now it's perfect, thank you! - bell_Kovac commented on March 12th 20 at 08:08
March 12th 20 at 08:01
Solution
'aaaaaa;bbbbb;;;ccccc;"dddd;eeee";ffff;"gggg;;hhhh";jjjj'
.match(/(".{0,}?";?)|(.{0,}?;)|(.+$)/g)
.map((item) => item.replace(/(^")|("?;?$)/g, "));
It turned out a universal thing to parse csv strings, thank you! - Jennings.Tremblay98 commented on March 12th 20 at 08:04

Find more questions by tags Regular expressionsJavaScript