Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript csv string with commas in fields

// Return array of string values, or NULL if CSV string not well formed.
function CSVtoArray(text) {
    var re_valid = /^s*(?:'[^']*(?:[Ss][^']*)*'|"[^"]*(?:[Ss][^"]*)*"|[^,'"s]*(?:s+[^,'"s]+)*)s*(?:,s*(?:'[^']*(?:[Ss][^']*)*'|"[^"]*(?:[Ss][^"]*)*"|[^,'"s]*(?:s+[^,'"s]+)*)s*)*$/;
    var re_value = /(?!s*$)s*(?:'([^']*(?:[Ss][^']*)*)'|"([^"]*(?:[Ss][^"]*)*)"|([^,'"s]*(?:s+[^,'"s]+)*))s*(?:,|$)/g;
    // Return NULL if input string is not well formed CSV string.
    if (!re_valid.test(text)) return null;
    var a = [];                     // Initialize array to receive values.
    text.replace(re_value, // "Walk" the string using replace with callback.
        function(m0, m1, m2, m3) {
            // Remove backslash from ' in single quoted values.
            if      (m1 !== undefined) a.push(m1.replace(/'/g, "'"));
            // Remove backslash from " in double quoted values.
            else if (m2 !== undefined) a.push(m2.replace(/"/g, '"'));
            else if (m3 !== undefined) a.push(m3);
            return ''; // Return empty string.
        });
    // Handle special case of empty last value.
    if (/,s*$/.test(text)) a.push('');
    return a;
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: valid filename regex 
Javascript :: return a boolean if a number is divisible by 10 javascript 
Javascript :: js fullscreen 
Javascript :: how to filter array objesct in express node js 
Javascript :: remove attribute javascript 
Javascript :: javascript 5 digit random number 
Javascript :: javascript modify url without reloading page 
Javascript :: class MyComponent extends React.Component { } ... what is the ES5 equivalent of this * 
Javascript :: Writing files in Node.js 
Javascript :: JavaScript performing an integer division 
Javascript :: angular build with configuration 
Javascript :: jest check binary 
Javascript :: Use the correct Date method to extract the year (four digits) out of a date object. 
Javascript :: identify unused node modules 
Javascript :: get everything between two characters regex 
Javascript :: sleep function javascript 
Javascript :: javasscript get the content inbetween a select tag 
Javascript :: js rect collision 
Javascript :: show 10 entries datatable jquery hide 
Javascript :: javascript eliminar items repetidos 
Javascript :: js element.scrollintoview navbar 
Javascript :: jquery check if element has child 
Javascript :: create a link javascript 
Javascript :: how to get file extension in javascript last index 
Javascript :: how to reset form values in jquery 
Javascript :: sinha crud template 
Javascript :: verta sample jalali laravel problem return object json 
Javascript :: react conditional classname 
Javascript :: get specific item from local storage 
Javascript :: number to money javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =