How to split lines of text into JavaScript Arrays where some elements are enclosed in quotes and contain commas?










0















I have Excel data converted to CSV. The original cells which contained commas are double-quoted in the CSV. For example, one row might look like:



Multiple Choice,"For a student that needing help, a teacher could:",Make it easier,Have them read independently,Use video,Summarize each paragraph,C



Without such quoted text containing commas I would just use JavaScript to break the string into array elements.



var theArray = theString.split(',');


But the fact that some of the pieces could be quoted and contain commas makes that difficult.



Is there a simple JavaScript way to break up this kind of text into Array elements, so in this case, the elements would be:



  • Multiple Choice

  • For a student that needing help, a teacher could:

  • Make it easier

  • Have them read independently

  • Use video

  • Summarize each paragraph

  • C









share|improve this question






















  • will it contain only double quotes ?

    – Artyom Amiryan
    Nov 13 '18 at 12:22











  • @Artyom CSV is actually a standard, so "hello", would be hello, were as "hello""" would be hello" etc. It would be trivial to make a CSV parser in pure Javascript, but sometimes it's a good idea to use a lib, in case of edge cases & performance. A quick google got me this -> papaparse.com/#unparse

    – Keith
    Nov 13 '18 at 12:25







  • 1





    Have you looked at the solutions in stackoverflow.com/questions/8493195/…

    – Tom Panning
    Nov 13 '18 at 12:29











  • @Keith ok thanks, will have a look

    – Artyom Amiryan
    Nov 13 '18 at 12:30











  • @TomPanning In that link, there is a warning, .. this solution does NOT fit the RFC 4180 definition of CSV and it also does NOT fit MS Excel format.

    – Keith
    Nov 13 '18 at 12:32















0















I have Excel data converted to CSV. The original cells which contained commas are double-quoted in the CSV. For example, one row might look like:



Multiple Choice,"For a student that needing help, a teacher could:",Make it easier,Have them read independently,Use video,Summarize each paragraph,C



Without such quoted text containing commas I would just use JavaScript to break the string into array elements.



var theArray = theString.split(',');


But the fact that some of the pieces could be quoted and contain commas makes that difficult.



Is there a simple JavaScript way to break up this kind of text into Array elements, so in this case, the elements would be:



  • Multiple Choice

  • For a student that needing help, a teacher could:

  • Make it easier

  • Have them read independently

  • Use video

  • Summarize each paragraph

  • C









share|improve this question






















  • will it contain only double quotes ?

    – Artyom Amiryan
    Nov 13 '18 at 12:22











  • @Artyom CSV is actually a standard, so "hello", would be hello, were as "hello""" would be hello" etc. It would be trivial to make a CSV parser in pure Javascript, but sometimes it's a good idea to use a lib, in case of edge cases & performance. A quick google got me this -> papaparse.com/#unparse

    – Keith
    Nov 13 '18 at 12:25







  • 1





    Have you looked at the solutions in stackoverflow.com/questions/8493195/…

    – Tom Panning
    Nov 13 '18 at 12:29











  • @Keith ok thanks, will have a look

    – Artyom Amiryan
    Nov 13 '18 at 12:30











  • @TomPanning In that link, there is a warning, .. this solution does NOT fit the RFC 4180 definition of CSV and it also does NOT fit MS Excel format.

    – Keith
    Nov 13 '18 at 12:32













0












0








0


0






I have Excel data converted to CSV. The original cells which contained commas are double-quoted in the CSV. For example, one row might look like:



Multiple Choice,"For a student that needing help, a teacher could:",Make it easier,Have them read independently,Use video,Summarize each paragraph,C



Without such quoted text containing commas I would just use JavaScript to break the string into array elements.



var theArray = theString.split(',');


But the fact that some of the pieces could be quoted and contain commas makes that difficult.



Is there a simple JavaScript way to break up this kind of text into Array elements, so in this case, the elements would be:



  • Multiple Choice

  • For a student that needing help, a teacher could:

  • Make it easier

  • Have them read independently

  • Use video

  • Summarize each paragraph

  • C









share|improve this question














I have Excel data converted to CSV. The original cells which contained commas are double-quoted in the CSV. For example, one row might look like:



Multiple Choice,"For a student that needing help, a teacher could:",Make it easier,Have them read independently,Use video,Summarize each paragraph,C



Without such quoted text containing commas I would just use JavaScript to break the string into array elements.



var theArray = theString.split(',');


But the fact that some of the pieces could be quoted and contain commas makes that difficult.



Is there a simple JavaScript way to break up this kind of text into Array elements, so in this case, the elements would be:



  • Multiple Choice

  • For a student that needing help, a teacher could:

  • Make it easier

  • Have them read independently

  • Use video

  • Summarize each paragraph

  • C






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 12:17









Doug LernerDoug Lerner

4531727




4531727












  • will it contain only double quotes ?

    – Artyom Amiryan
    Nov 13 '18 at 12:22











  • @Artyom CSV is actually a standard, so "hello", would be hello, were as "hello""" would be hello" etc. It would be trivial to make a CSV parser in pure Javascript, but sometimes it's a good idea to use a lib, in case of edge cases & performance. A quick google got me this -> papaparse.com/#unparse

    – Keith
    Nov 13 '18 at 12:25







  • 1





    Have you looked at the solutions in stackoverflow.com/questions/8493195/…

    – Tom Panning
    Nov 13 '18 at 12:29











  • @Keith ok thanks, will have a look

    – Artyom Amiryan
    Nov 13 '18 at 12:30











  • @TomPanning In that link, there is a warning, .. this solution does NOT fit the RFC 4180 definition of CSV and it also does NOT fit MS Excel format.

    – Keith
    Nov 13 '18 at 12:32

















  • will it contain only double quotes ?

    – Artyom Amiryan
    Nov 13 '18 at 12:22











  • @Artyom CSV is actually a standard, so "hello", would be hello, were as "hello""" would be hello" etc. It would be trivial to make a CSV parser in pure Javascript, but sometimes it's a good idea to use a lib, in case of edge cases & performance. A quick google got me this -> papaparse.com/#unparse

    – Keith
    Nov 13 '18 at 12:25







  • 1





    Have you looked at the solutions in stackoverflow.com/questions/8493195/…

    – Tom Panning
    Nov 13 '18 at 12:29











  • @Keith ok thanks, will have a look

    – Artyom Amiryan
    Nov 13 '18 at 12:30











  • @TomPanning In that link, there is a warning, .. this solution does NOT fit the RFC 4180 definition of CSV and it also does NOT fit MS Excel format.

    – Keith
    Nov 13 '18 at 12:32
















will it contain only double quotes ?

– Artyom Amiryan
Nov 13 '18 at 12:22





will it contain only double quotes ?

– Artyom Amiryan
Nov 13 '18 at 12:22













@Artyom CSV is actually a standard, so "hello", would be hello, were as "hello""" would be hello" etc. It would be trivial to make a CSV parser in pure Javascript, but sometimes it's a good idea to use a lib, in case of edge cases & performance. A quick google got me this -> papaparse.com/#unparse

– Keith
Nov 13 '18 at 12:25






@Artyom CSV is actually a standard, so "hello", would be hello, were as "hello""" would be hello" etc. It would be trivial to make a CSV parser in pure Javascript, but sometimes it's a good idea to use a lib, in case of edge cases & performance. A quick google got me this -> papaparse.com/#unparse

– Keith
Nov 13 '18 at 12:25





1




1





Have you looked at the solutions in stackoverflow.com/questions/8493195/…

– Tom Panning
Nov 13 '18 at 12:29





Have you looked at the solutions in stackoverflow.com/questions/8493195/…

– Tom Panning
Nov 13 '18 at 12:29













@Keith ok thanks, will have a look

– Artyom Amiryan
Nov 13 '18 at 12:30





@Keith ok thanks, will have a look

– Artyom Amiryan
Nov 13 '18 at 12:30













@TomPanning In that link, there is a warning, .. this solution does NOT fit the RFC 4180 definition of CSV and it also does NOT fit MS Excel format.

– Keith
Nov 13 '18 at 12:32





@TomPanning In that link, there is a warning, .. this solution does NOT fit the RFC 4180 definition of CSV and it also does NOT fit MS Excel format.

– Keith
Nov 13 '18 at 12:32












2 Answers
2






active

oldest

votes


















1














You'll probably have to use some regex to setup a more detailed selector.



Try the following:



theString.split(/(?:,)(?=(?:[^"]|"[^"]*")*$)/)


Which would result in



['Multiple Choice', '"For a student that needing help, a teacher could:"',
'Make it easier', 'Have them read independently', 'Use video',
'Summarize each paragraph', 'C']





share|improve this answer

























  • The second array element is wrong for a CSV, it should not contain the "

    – Keith
    Nov 13 '18 at 12:28











  • @Keith That shouldn't matter for Javascript. If it becomes an issue you can easily add a second operation to strip these quotemarks.

    – Raven
    Nov 13 '18 at 12:30











  • But what if the quotes were there?

    – Keith
    Nov 13 '18 at 12:30











  • Then you can add .map(s=>s.replace(/^"|"$/g,"")); to remove start and trailing "'s. Those quotation marks are just artifacts of parsing the CSV and don't affect javascript strings

    – Raven
    Nov 13 '18 at 12:34







  • 1





    I think, as Raven mentioned above, I could strip out the outer quotes from the results and replace instances of escaped double-quotes "" with just " and call it a day. Thank you!

    – Doug Lerner
    Nov 13 '18 at 13:34


















0














I believe this regex answers the needed result:
/(?:([^,"]+)|(?:"([^"]+)"))/
You can use that in Raven's answer.






share|improve this answer























  • For that solution, that does seem to cause extra elements when there are internal quotes. For example, with Multiple Choice,"When scoring the TAGPEC, what number score does an ""Emerging"" item receive?",0,1,2,3,4,B the word Emerging gets split into a separate element.

    – Doug Lerner
    Nov 13 '18 at 13:07










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280853%2fhow-to-split-lines-of-text-into-javascript-arrays-where-some-elements-are-enclos%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You'll probably have to use some regex to setup a more detailed selector.



Try the following:



theString.split(/(?:,)(?=(?:[^"]|"[^"]*")*$)/)


Which would result in



['Multiple Choice', '"For a student that needing help, a teacher could:"',
'Make it easier', 'Have them read independently', 'Use video',
'Summarize each paragraph', 'C']





share|improve this answer

























  • The second array element is wrong for a CSV, it should not contain the "

    – Keith
    Nov 13 '18 at 12:28











  • @Keith That shouldn't matter for Javascript. If it becomes an issue you can easily add a second operation to strip these quotemarks.

    – Raven
    Nov 13 '18 at 12:30











  • But what if the quotes were there?

    – Keith
    Nov 13 '18 at 12:30











  • Then you can add .map(s=>s.replace(/^"|"$/g,"")); to remove start and trailing "'s. Those quotation marks are just artifacts of parsing the CSV and don't affect javascript strings

    – Raven
    Nov 13 '18 at 12:34







  • 1





    I think, as Raven mentioned above, I could strip out the outer quotes from the results and replace instances of escaped double-quotes "" with just " and call it a day. Thank you!

    – Doug Lerner
    Nov 13 '18 at 13:34















1














You'll probably have to use some regex to setup a more detailed selector.



Try the following:



theString.split(/(?:,)(?=(?:[^"]|"[^"]*")*$)/)


Which would result in



['Multiple Choice', '"For a student that needing help, a teacher could:"',
'Make it easier', 'Have them read independently', 'Use video',
'Summarize each paragraph', 'C']





share|improve this answer

























  • The second array element is wrong for a CSV, it should not contain the "

    – Keith
    Nov 13 '18 at 12:28











  • @Keith That shouldn't matter for Javascript. If it becomes an issue you can easily add a second operation to strip these quotemarks.

    – Raven
    Nov 13 '18 at 12:30











  • But what if the quotes were there?

    – Keith
    Nov 13 '18 at 12:30











  • Then you can add .map(s=>s.replace(/^"|"$/g,"")); to remove start and trailing "'s. Those quotation marks are just artifacts of parsing the CSV and don't affect javascript strings

    – Raven
    Nov 13 '18 at 12:34







  • 1





    I think, as Raven mentioned above, I could strip out the outer quotes from the results and replace instances of escaped double-quotes "" with just " and call it a day. Thank you!

    – Doug Lerner
    Nov 13 '18 at 13:34













1












1








1







You'll probably have to use some regex to setup a more detailed selector.



Try the following:



theString.split(/(?:,)(?=(?:[^"]|"[^"]*")*$)/)


Which would result in



['Multiple Choice', '"For a student that needing help, a teacher could:"',
'Make it easier', 'Have them read independently', 'Use video',
'Summarize each paragraph', 'C']





share|improve this answer















You'll probably have to use some regex to setup a more detailed selector.



Try the following:



theString.split(/(?:,)(?=(?:[^"]|"[^"]*")*$)/)


Which would result in



['Multiple Choice', '"For a student that needing help, a teacher could:"',
'Make it easier', 'Have them read independently', 'Use video',
'Summarize each paragraph', 'C']






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 12:35

























answered Nov 13 '18 at 12:25









RavenRaven

6752822




6752822












  • The second array element is wrong for a CSV, it should not contain the "

    – Keith
    Nov 13 '18 at 12:28











  • @Keith That shouldn't matter for Javascript. If it becomes an issue you can easily add a second operation to strip these quotemarks.

    – Raven
    Nov 13 '18 at 12:30











  • But what if the quotes were there?

    – Keith
    Nov 13 '18 at 12:30











  • Then you can add .map(s=>s.replace(/^"|"$/g,"")); to remove start and trailing "'s. Those quotation marks are just artifacts of parsing the CSV and don't affect javascript strings

    – Raven
    Nov 13 '18 at 12:34







  • 1





    I think, as Raven mentioned above, I could strip out the outer quotes from the results and replace instances of escaped double-quotes "" with just " and call it a day. Thank you!

    – Doug Lerner
    Nov 13 '18 at 13:34

















  • The second array element is wrong for a CSV, it should not contain the "

    – Keith
    Nov 13 '18 at 12:28











  • @Keith That shouldn't matter for Javascript. If it becomes an issue you can easily add a second operation to strip these quotemarks.

    – Raven
    Nov 13 '18 at 12:30











  • But what if the quotes were there?

    – Keith
    Nov 13 '18 at 12:30











  • Then you can add .map(s=>s.replace(/^"|"$/g,"")); to remove start and trailing "'s. Those quotation marks are just artifacts of parsing the CSV and don't affect javascript strings

    – Raven
    Nov 13 '18 at 12:34







  • 1





    I think, as Raven mentioned above, I could strip out the outer quotes from the results and replace instances of escaped double-quotes "" with just " and call it a day. Thank you!

    – Doug Lerner
    Nov 13 '18 at 13:34
















The second array element is wrong for a CSV, it should not contain the "

– Keith
Nov 13 '18 at 12:28





The second array element is wrong for a CSV, it should not contain the "

– Keith
Nov 13 '18 at 12:28













@Keith That shouldn't matter for Javascript. If it becomes an issue you can easily add a second operation to strip these quotemarks.

– Raven
Nov 13 '18 at 12:30





@Keith That shouldn't matter for Javascript. If it becomes an issue you can easily add a second operation to strip these quotemarks.

– Raven
Nov 13 '18 at 12:30













But what if the quotes were there?

– Keith
Nov 13 '18 at 12:30





But what if the quotes were there?

– Keith
Nov 13 '18 at 12:30













Then you can add .map(s=>s.replace(/^"|"$/g,"")); to remove start and trailing "'s. Those quotation marks are just artifacts of parsing the CSV and don't affect javascript strings

– Raven
Nov 13 '18 at 12:34






Then you can add .map(s=>s.replace(/^"|"$/g,"")); to remove start and trailing "'s. Those quotation marks are just artifacts of parsing the CSV and don't affect javascript strings

– Raven
Nov 13 '18 at 12:34





1




1





I think, as Raven mentioned above, I could strip out the outer quotes from the results and replace instances of escaped double-quotes "" with just " and call it a day. Thank you!

– Doug Lerner
Nov 13 '18 at 13:34





I think, as Raven mentioned above, I could strip out the outer quotes from the results and replace instances of escaped double-quotes "" with just " and call it a day. Thank you!

– Doug Lerner
Nov 13 '18 at 13:34













0














I believe this regex answers the needed result:
/(?:([^,"]+)|(?:"([^"]+)"))/
You can use that in Raven's answer.






share|improve this answer























  • For that solution, that does seem to cause extra elements when there are internal quotes. For example, with Multiple Choice,"When scoring the TAGPEC, what number score does an ""Emerging"" item receive?",0,1,2,3,4,B the word Emerging gets split into a separate element.

    – Doug Lerner
    Nov 13 '18 at 13:07















0














I believe this regex answers the needed result:
/(?:([^,"]+)|(?:"([^"]+)"))/
You can use that in Raven's answer.






share|improve this answer























  • For that solution, that does seem to cause extra elements when there are internal quotes. For example, with Multiple Choice,"When scoring the TAGPEC, what number score does an ""Emerging"" item receive?",0,1,2,3,4,B the word Emerging gets split into a separate element.

    – Doug Lerner
    Nov 13 '18 at 13:07













0












0








0







I believe this regex answers the needed result:
/(?:([^,"]+)|(?:"([^"]+)"))/
You can use that in Raven's answer.






share|improve this answer













I believe this regex answers the needed result:
/(?:([^,"]+)|(?:"([^"]+)"))/
You can use that in Raven's answer.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 12:48









Yoav MoranYoav Moran

143




143












  • For that solution, that does seem to cause extra elements when there are internal quotes. For example, with Multiple Choice,"When scoring the TAGPEC, what number score does an ""Emerging"" item receive?",0,1,2,3,4,B the word Emerging gets split into a separate element.

    – Doug Lerner
    Nov 13 '18 at 13:07

















  • For that solution, that does seem to cause extra elements when there are internal quotes. For example, with Multiple Choice,"When scoring the TAGPEC, what number score does an ""Emerging"" item receive?",0,1,2,3,4,B the word Emerging gets split into a separate element.

    – Doug Lerner
    Nov 13 '18 at 13:07
















For that solution, that does seem to cause extra elements when there are internal quotes. For example, with Multiple Choice,"When scoring the TAGPEC, what number score does an ""Emerging"" item receive?",0,1,2,3,4,B the word Emerging gets split into a separate element.

– Doug Lerner
Nov 13 '18 at 13:07





For that solution, that does seem to cause extra elements when there are internal quotes. For example, with Multiple Choice,"When scoring the TAGPEC, what number score does an ""Emerging"" item receive?",0,1,2,3,4,B the word Emerging gets split into a separate element.

– Doug Lerner
Nov 13 '18 at 13:07

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280853%2fhow-to-split-lines-of-text-into-javascript-arrays-where-some-elements-are-enclos%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo