'How to Split my String in Javascript for special case
i want my string using "," (comma) but my String have some special case please help me
input : a,"d,e,f",g //string in csv`
var myarray=str.spilt(",");
output: myarray[0] = a ,myarray[1]="d ,myarray[3]=e,myarray[4]=f.....
required output : myarray[0] =a ,myarray[1]="d,e,f",myarray[2] = g
please help me... thanks
Solution 1:[1]
This would be a lot smoother with a mapcat operator in JS.
let input = 'a,"d,e,f",g'
var result = []
input.split('"').forEach((e, i) => {
result = result.concat( i % 2 == 0 ? e.split(',') : e )
})
result = result.filter(e => e != "")
As ChristianFigueroa pointed out: This won't work for cases where the quotes don't surround the whole field: value,val"ue", "value"
However if that is not important for you: jsperf/speedtest
Solution 2:[2]
Strongly advise to parse CSV with a tested CSV-parser library instead of any regex.
CSV-parsers for JavaScript
Among the common ones example:
- Papa Parse
- JQuery-CSV
- D3.js DSV module, including a CSV parser
Example usages
Your example using
- .. Papa Parse, can auto-detect the delimiter and has already a default for quoted-strings:
quoteChar: '"'(like yours). Try it online:
var csvInput = 'a,"d,e,f",g';
var results = Papa.parse(csvString);
console.log(results);
Output (copied from console):
{
data: [
"a",
"d,e,f",
"g"
],
errors: []
meta: {delimiter: ",", linebreak: "?", aborted: false, truncated: false, cursor: 11}
}
- .. JQuery-CSV, you can try it online:
var input = 'a,"d,e,f",g';
var result = $.csv.toArray(input);
console.log(result);
// even configurable
$.csv.toArrays(input, {
delimiter: "\"", // already the default, custom value delimiter character
separator: ',', // already the default, custom field separator character
});
Output:
[
"a",
"d,e,f",
"g"
]
Disadvantage of regular-expressions for CSV parsing
The regex for CSV-parsing can be either simple like split(',') or long, cumbersome and fragile - catasrophic backracking like described on regular-expressions.info: "A Real Example: Matching CSV Records".
Furthermore:
- a regex is not easy to configure.
- a regex is hard to document, maintain, modularize or extend.
If you still want to try, read
Related questions
Solution 3:[3]
you need to use regexp
var str = 'a,"d,e,f",g';
console.log(str.split(/,"|",/));
Solution 4:[4]
myarray = []
while (str) {
start = str;
str = str.replace(/(".*?"|[^",]+|)($|,)/, function(match, group1) {
myarray.push(group1);
return "";
});
if (str == start) break;
}
The code above goes through the CSV string, adding each value it can to the array myarray. If the value has a " as it's first character (may contain commas), it will match until it reaches the next closing ". Otherwise, the regex will just look for the next comma. If it finds something like a value without a closing ", it will automatically stop and myarray will include all the values up to that point that it failed. Empty values (two adjacent commas) will also work.
Solution 5:[5]
You can clean up the CSV with adding delimiters " to create new columns. This is in excel though but I'm not sure how you parse this data so might not work for you. Or str_replace() them and then str.split?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | hc_dev |
| Solution 3 | raksa |
| Solution 4 | |
| Solution 5 | Muppet |
