Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your base pattern contains some flaws, and not easily extensible: Delimiters are always prefixed by a <code>\\</code>, even when they shouldn't.</p> <p>I've rewritten the code, to make it mor ereliable.Multiple quotation types are supported, and delimiters are properly escaped.</p> <p><strong>Fiddle: <a href="http://jsfiddle.net/qz53J/" rel="nofollow">http://jsfiddle.net/qz53J/</a></strong></p> <pre><code>function CSVToArray( strData, strDelimiter ){ // Properly escape the delimiter, if existent. // If no delimiter is given, use a comma strDelimiter = (strDelimiter || ",").replace(/([[^$.|?*+(){}])/g, '\\$1'); //What are the quotation characters? "' var quotes = "\"'"; // Create a regular expression to parse the CSV values. // match[1] = Contains the delimiter if the RegExp is not at the begin // match[2] = quote, if any // match[3] = string inside quotes, if match[2] exists // match[4] = non-quoted strings var objPattern = new RegExp( // Delimiter or marker of new row "(?:(" + strDelimiter + ")|[\\n\\r]|^)" + // Quoted fields "(?:([" + quotes + "])((?:[^" + quotes + "]+|(?!\\2).|\\2\\2)*)\\2" + // Standard fields "|([^" + quotes + strDelimiter + "\\n\\r]*))" , "gi"); // Create a matrix (2d array) to hold data, which will be returned. var arrData = []; // Execute the RegExp until no match is found var arrMatches; while ( arrMatches = objPattern.exec( strData ) ){ // If the first group of the RegExp does is empty, no delimiter is // matched. This only occurs at the beginning of a new row if ( !arrMatches[ 1 ] ){ // Add an empty row to our data array. arrData.push( [] ); } var quote = arrMatches[ 2 ] if ( quote ){ // We found a quoted value. When we capture // this value, unescape any double quotes. var strMatchedValue = arrMatches[ 3 ].replace( new RegExp( quote + quote, "g" ), quote ); } else { // We found a non-quoted value. var strMatchedValue = arrMatches[ 4 ]; } // Add the found value to the array arrData[ arrData.length - 1 ].push( strMatchedValue ); } // Return the parsed data. return arrData; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload