Home > Web Front-end > JS Tutorial > How Can Regular Expressions Solve the Problem of Parsing CSV Strings with Commas in Data Fields in JavaScript?

How Can Regular Expressions Solve the Problem of Parsing CSV Strings with Commas in Data Fields in JavaScript?

DDD
Release: 2024-12-07 04:14:10
Original
202 people have browsed it

How Can Regular Expressions Solve the Problem of Parsing CSV Strings with Commas in Data Fields in JavaScript?

Parsable CSV Strings in JavaScript

Parsing CSV with Commas in Data

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:

The Regex Solution

  1. Define the CSV String Structure: Determine the characteristics of a valid CSV string, including quoted strings, escaped characters, and data formatting.
  2. Regex for CSV Validation: Create a regex that validates whether a given string meets these criteria.
  3. Regex for Value Extraction: Construct a regex to match and extract individual values from the valid CSV string, considering quoted and unquoted strings and trailing commas.

Implementation

// 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;
};
Copy after login

Example Usage

// 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']
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template