const regex = /,?\s?("[^"]*,[^"]*"|[^",]*)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp(',?\\s?("[^"]*,[^"]*"|[^",]*)', 'gm')
const str = `St. Lawrence County,NY,,2,,Attainment Plan/Maintenance Plan,37,,1852,Nonattainment,,,,11922,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,2,,5,,0,0,,,,,,,,,,,,,,11922,,,,St. Lawrence County,4700,NY,NY,SO2.2010.St_Lawrence_Co,SO2.2010.St_Lawrence_Co.NY,,,,,,,,,,
Howard County,TX,,6,,Attainment Plan/Maintenance Plan,37,,1853,Nonattainment,,,,281,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,6,,5,,0,0,,,,,,,,,,,,,,281,,,,Howard County,4710,TX,TX,SO2.2010.Howard_Co,SO2.2010.Howard_Co.TX,,,,,,,,,,
Hutchinson County,TX,,6,,Attainment Plan/Maintenance Plan,37,,1854,Nonattainment,,,,14593,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,6,,5,,0,0,,,,,,,,,,,,,,14593,,,,Hutchinson County,4720,TX,TX,SO2.2010.Hutchinson_Co,SO2.2010.Hutchinson_Co.TX,,,,,,,,,,
Navarro County,TX,,6,,Attainment Plan/Maintenance Plan,37,,1855,Nonattainment,,,,1593,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,6,,5,,0,0,,,,,,,,,,,,,,1593,,,,Navarro County,4730,TX,TX,SO2.2010.Navarro_Co,SO2.2010.Navarro_Co.TX,,,,,,,,,,
Giles County,VA,,3,,Attainment Plan/Maintenance Plan,37,,1856,Nonattainment,,,,183,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,3,,5,,0,0,,,,,,,,,,,,,,183,,,,Giles County,4740,VA,VA,SO2.2010.Giles_Co,SO2.2010.Giles_Co.VA,,,,,,,,,,
New Madrid County,MO,,7,,Attainment Plan/Maintenance Plan,37,,1851,Nonattainment,,,,2,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,7,,5,,0,0,,,,,,,,,,,,,,2,,,,New Madrid County,4690,MO,MO,SO2.2010.New_Madrid_Co,SO2.2010.New_Madrid_Co.MO,,,,,,,,,,
Henderson-Webster Counties,KY,,4,,Attainment Plan/Maintenance Plan,37,,1850,Nonattainment,,,,6932,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,4,,5,,0,0,,,,,,,,,,,,,,6932,,,,Henderson-Webster Counties,4680,KY,KY,SO2.2010.Henderson_Webster_Cos,SO2.2010.Henderson_Webster_Cos.KY,,,,,,,,,,
Whatcom County,WA,,10,,Attainment Plan/Maintenance Plan,37,,1857,Nonattainment,,,,32,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,10,,5,,0,0,,,,,,,,,,,,,,32,,,,Whatcom County,4750,WA,WA,SO2.2010.Whatcom_Co,SO2.2010.Whatcom_Co.WA,,,,,,,,,,`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions