Similar Question:
JavaScript code for parsing CSV data
There is a string that looks like this:
"display, Name" <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d49584e497d49584e49135e5250">[email protected]</a>>, display" Name <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97e3f2e4e3d7e3f2e4e3b9f4f8fa">[email protected]</a>>, <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d29382e291d29382e29733e3230">[email protected]</a>
The goal is to split this string into an array.
array[0] = "\"display, Name\" <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98ecfdebecd8ecfdebecb6fbf7f5">[email protected]</a>>"
array[1] = "display" Name <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9beffee8efdbeffee8efb5f8f4f6">[email protected]</a>>"
array[2] = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="116574626551657462653f727e7c">[email protected]</a>"
This is the current code being used:
var comma = inQuotes = false;
for(var i=0;i<str.length;i++) {
if (str[i] == '"') inQuotes = !inQuotes;
comma = (str[i] == "," && !inQuotes) ? true : false;
item += (!comma) ? str[i] : "";
if(comma || i == str.length-1) {
items.push(item);
item = "";
}
}
An issue arises when there is a single double quote without a closing one within a string.
Your assistance on this matter would be greatly appreciated. Thank you.