node_saini
node_saini

Reputation: 812

Split by comma while ignoring special characters

I want to split it comma but not the escaped string. Also remove slash from it.

var str = "02-Dentist,\"***IN-Den-WV,VA,SC,TN,LA-122016\",Riverside,888-885-6112,5,1,20.00%,1690000"

Current Output

["02-Dentist,\"***IN-Den-WV,VA,SC,TN,LA-122016\",Riverside,888-885-6112,5,1,20.00%,1690000"

Expected Output

["02-Dentist","***IN-Den-WV,VA,SC,TN,LA-122016","Riverside","888-888-9999","5","1","20.00%","1690000"]

Tried Code

var replaced = str.replace(/[^\\],/,"$09").split("$09")

Upvotes: 2

Views: 809

Answers (2)

Vladu Ionut
Vladu Ionut

Reputation: 8193

var str = "02-Dentist,\"***IN-Den-WV,VA,SC,TN,LA-122016\",Riverside,888-885-6112,5,1,20.00%,1690000";
var result = str.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/g);
console.log(result);

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

It seems you need to tokenize a string into what is between double quotes, or between commas. I assume the fields are delimited with commas, and may be enclosed with double quotes with escaped quotes/entities inside.

You may use

var m,res = [];
var str = "02-Dentist,\"***IN-Den-WV,VA,SC,TN,LA-122016\",Riverside,888-885-6112,5,1,20.00%,1690000";
var re = /"([^"\\]*(?:\\.[^"\\]*)*)"|[^,]+/g;
while((m=re.exec(str)) !== null){
  if (m[1]) 
    res.push(m[1]);
  else  
    res.push(m[0]); 
}
console.log(res);

The pattern is:

/"([^"\\]*(?:\\.[^"\\]*)*)"|[^,]+/g

See its online demo. It matches the double quoted substring together with any escape entities inside and captures what is in between the quotes (with "([^"\\]*(?:\\.[^"\\]*)*)") and also matches 1+ characters other than , with [^,]+.

With if (m[1]), we can check if Group 1 matched and if yes, the contents inside Group 1 (m[1]) is pushed to the final array. If not, the whole match value is pushed to the array (m[0]).

If there are no escape entities in your input, you may even use

/"([^"]*)"|[^,]+/g

See the regex demo

Upvotes: 1

Related Questions