How can I parse a CSV string with JavaScript, which contains comma in data?

前端 未结 17 1178
不知归路
不知归路 2020-11-22 01:52

I have the following type of string

var string = "\'string, duppi, du\', 23, lala"

I want to split the string into an array on each

17条回答
  •  野性不改
    2020-11-22 02:04

    If you can have your quote delimiter be double quotes, then this is a duplicate of Example JavaScript code to parse CSV data.

    You can either translate all single-quotes to double-quotes first:

    string = string.replace( /'/g, '"' );
    

    ...or you can edit the regex in that question to recognize single-quotes instead of double-quotes:

    // Quoted fields.
    "(?:'([^']*(?:''[^']*)*)'|" +
    

    However, this assumes certain markup that is not clear from your question. Please clarify what all the various possibilities of markup can be, per my comment on your question.

提交回复
热议问题