Home >Web Front-end >JS Tutorial >How Can Regular Expressions Solve the Problem of Parsing CSV Strings with Commas in Data Fields in JavaScript?
In this situation, you require a solution that distinguishes commas within quotation marks from those separating values. A regular expression is the ideal tool for this task. Here's how you can approach this:
// Regex to validate a CSV string const re_valid = /^\s*(?:'[^'\]*(?:\[\S\s][^'\]*)*'|"[^"\]*(?:\[\S\s][^"\]*)*"|[^,'"\s\]*(?:\s+[^,'"\s\]+)*)\s*(?:,\s*(?:'[^'\]*(?:\[\S\s][^'\]*)*'|"[^"\]*(?:\[\S\s][^"\]*)*"|[^,'"\s\]*(?:\s+[^,'"\s\]+)*)\s*)*$/ // Regex to parse one value from a valid CSV string const re_value = /(?!\s*$)\s*(?:'([^'\]*(?:\[\S\s][^'\]*)*)'|"([^"\]*(?:\[\S\s][^"\]*)*)"|([^,'"\s\]*(?:\s+[^,'"\s\]+)*))\s*(?:,|$)/g // Function to parse CSV string into array const CSVtoArray = (text) => { // Validate CSV string if (!re_valid.test(text)) return null; const values = []; text.replace(re_value, (m0, m1, m2, m3) => { // Handle single-quoted values if (m1 !== undefined) values.push(m1.replace(/\'/g, "'")); // Handle double-quoted values else if (m2 !== undefined) values.push(m2.replace(/\"/g, '"')); // Handle unquoted values else if (m3 !== undefined) values.push(m3); return ''; }); // Handle special case of empty last value if (/,\s*$/.test(text)) values.push(''); return values; };
// Sample CSV string with commas in data const csvString = "'string, with comma', 23, 'lala'"; // Parse the CSV string into an array of values const values = CSVtoArray(csvString); console.log(values); // ['string, with comma', '23', 'lala']
This approach effectively parses the CSV string, preserving the integrity of quoted values and correctly handling commas within data fields. You can adjust the regex to customize the handling of specific CSV string variations or formatting requirements.
The above is the detailed content of How Can Regular Expressions Solve the Problem of Parsing CSV Strings with Commas in Data Fields in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!