以下经过测试的 JavaScript 函数可以解决问题:
分号分隔值:
function splitByUnescapedSemicolons(text) {
var a = []; // Array to receive results.
if (text === '') return a; // Special empty string case.
// Push first (possibly last) value.
text = text.replace(/^[^;\\]*(?:\\[\S\s][^;\\]*)*(?=;|$)/,
function(m0){a.push(m0); return '';});
// Push any 2nd, 3rd, remaining values.
text = text.replace(/;([^;\\]*(?:\\[\S\s][^;\\]*)*)/g,
function(m0, m1){a.push(m1); return '';});
return a;
}
该解决方案正确处理转义的分号(并且也转义了其他任何内容,包括转义转义)。
示例数据:
"" == [];
";" == ['', ''];
"\;" == ['\;'];
"\\;" == ['\\', ''];
"one;two" == ['one', 'two'];
"abc;def;ghi\;jk" == ['abc', 'def', 'ghi\;jk'];
"abc;def;ghi\\;jk" == ['abc', 'def', 'ghi\\', 'jk'];