我想用逗号分隔,但不是转义字符串。同时删除斜杠。
var str = "02-Dentist,\"***IN-Den-WV,VA,SC,TN,LA-122016\",Riverside,888-885-6112,5,1,20.00%,1690000"
当前输出
["02-Dentist,\"***IN-Den-WV,VA,SC,TN,LA-122016\",Riverside,888-885-6112,5,1,20.00%,1690000"
预期产出
["02-Dentist","***IN-Den-WV,VA,SC,TN,LA-122016","Riverside","888-888-9999","5","1","20.00%","1690000"]
尝试代码
var replaced = str.replace(/[^\\],/,"$09").split("$09")
答案 0 :(得分:1)
似乎您需要将字符串标记为双引号之间或逗号之间。我假设这些字段用逗号分隔,并且可以用双引号括起来,里面有转义的引号/实体。
您可以使用
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);

模式是:
/"([^"\\]*(?:\\.[^"\\]*)*)"|[^,]+/g
见its online demo。它将双引号子字符串与内部的任何转义实体匹配,并且捕获引号之间的内容(使用"([^"\\]*(?:\\.[^"\\]*)*)"
),并且还匹配,
以外的1 +个字符{ {1}}。
使用[^,]+
,我们可以检查组1是否匹配,如果是,则组1(if (m[1])
)内的内容被推送到最终数组。如果不是,则将整个匹配值推送到数组(m[1]
)。
如果输入中没有转义实体,您甚至可以使用
m[0]
请参阅regex demo
答案 1 :(得分:1)
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);