我有以下类型的字符串
var string = "'string, duppi, du', 23, lala"
我想将字符串按逗号拆分为数组,但仅限于单引号之外的逗号。我无法找到适合拆分的正则表达式...string.split(/,/)
会给我
["'string", " duppi", " du'", " 23", " lala"]
但结果应该是:
["string, duppi, du", "23", "lala"]
有没有跨浏览器的解决方案?
我有以下类型的字符串
var string = "'string, duppi, du', 23, lala"
我想将字符串按逗号拆分为数组,但仅限于单引号之外的逗号。我无法找到适合拆分的正则表达式...string.split(/,/)
会给我
["'string", " duppi", " du'", " 23", " lala"]
但结果应该是:
["string, duppi, du", "23", "lala"]
有没有跨浏览器的解决方案?
2014-12-01 更新:下面的答案仅适用于一种非常特定的 CSV 格式。正如 DG 在评论中正确指出的那样,此解决方案不符合 RFC 4180 定义的 CSV 格式,也不符合 MS Excel 格式。此解决方案仅演示了如何解析包含字符串类型混合的一个(非标准)CSV 输入行,其中字符串可能包含转义引号和逗号。
正如 austincheney 正确指出的那样,如果您希望正确处理可能包含转义字符的引号字符串,则确实需要从头到尾解析字符串。此外,OP 没有清楚地定义“CSV 字符串”是什么。首先,我们必须定义什么构成有效 CSV 字符串及其各个值。
为了讨论目的,一个“CSV 字符串”由零个或多个值组成,其中多个值由逗号分隔。每个值可以包含:
规则/注释:
'that\'s cool'
。\'
的反斜杠。\"
的反斜杠。一种JavaScript函数,可将有效的CSV字符串(如上所定义)转换为字符串值的数组。
这个解决方案使用的正则表达式非常复杂。而且(在我看来)所有的非平凡正则表达式都应该以自由空间模式呈现,包含大量注释和缩进。不幸的是,JavaScript 不允许自由空间模式。因此,此解决方案实现的正则表达式首先以本地正则表达式语法呈现(使用 Python 的便捷的 r'''...'''
原始多行字符串语法表示)。
首先,这里是一个正则表达式,用于验证 CVS 字符串是否满足上述要求:
re_valid = r"""
# Validate a CSV string having single, double or un-quoted values.
^ # Anchor to start of string.
\s* # Allow whitespace before value.
(?: # Group for value alternatives.
'[^'\\]*(?:\\[\S\s][^'\\]*)*' # Either Single quoted string,
| "[^"\\]*(?:\\[\S\s][^"\\]*)*" # or Double quoted string,
| [^,'"\s\\]*(?:\s+[^,'"\s\\]+)* # or Non-comma, non-quote stuff.
) # End group of value alternatives.
\s* # Allow whitespace after value.
(?: # Zero or more additional values
, # Values separated by a comma.
\s* # Allow whitespace before value.
(?: # Group for value alternatives.
'[^'\\]*(?:\\[\S\s][^'\\]*)*' # Either Single quoted string,
| "[^"\\]*(?:\\[\S\s][^"\\]*)*" # or Double quoted string,
| [^,'"\s\\]*(?:\s+[^,'"\s\\]+)* # or Non-comma, non-quote stuff.
) # End group of value alternatives.
\s* # Allow whitespace after value.
)* # Zero or more additional values
$ # Anchor to end of string.
"""
re_value = r"""
# Match one value in valid CSV string.
(?!\s*$) # Don't match empty last value.
\s* # Strip whitespace before value.
(?: # Group for value alternatives.
'([^'\\]*(?:\\[\S\s][^'\\]*)*)' # Either $1: Single quoted string,
| "([^"\\]*(?:\\[\S\s][^"\\]*)*)" # or $2: Double quoted string,
| ([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*) # or $3: Non-comma, non-quote stuff.
) # End group of value alternatives.
\s* # Strip whitespace after value.
(?:,|$) # Field ends on comma or EOS.
"""
// Return array of string values, or NULL if CSV string not well formed.
function CSVtoArray(text) {
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
var re_value = /(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g;
// Return NULL if input string is not well formed CSV string.
if (!re_valid.test(text)) return null;
var a = []; // Initialize array to receive values.
text.replace(re_value, // "Walk" the string using replace with callback.
function(m0, m1, m2, m3) {
// Remove backslash from \' in single quoted values.
if (m1 !== undefined) a.push(m1.replace(/\\'/g, "'"));
// Remove backslash from \" in double quoted values.
else if (m2 !== undefined) a.push(m2.replace(/\\"/g, '"'));
else if (m3 !== undefined) a.push(m3);
return ''; // Return empty string.
});
// Handle special case of empty last value.
if (/,\s*$/.test(text)) a.push('');
return a;
};
console.log('Test 1: Test string from original question.');
console.log(CSVtoArray("'string, duppi, du', 23, lala"));
console.log('Test 2: Empty CSV string.');
console.log(CSVtoArray(""));
console.log('Test 3: CSV string with two empty values.');
console.log(CSVtoArray(","));
console.log('Test 4: Double quoted CSV string having single quoted values.');
console.log(CSVtoArray("'one','two with escaped \' single quote', 'three, with, commas'"));
console.log('Test 5: Single quoted CSV string having double quoted values.');
console.log(CSVtoArray('"one","two with escaped \" double quote", "three, with, commas"'));
console.log('Test 6: CSV string with whitespace in and around empty and non-empty values.');
console.log(CSVtoArray(" one , 'two' , , ' four' ,, 'six ', ' seven ' , "));
console.log('Test 7: Not valid');
console.log(CSVtoArray("one, that's me!, escaped \, comma"));
此解决方案要求CSV字符串是“有效的”。例如,未引用的值不能包含反斜杠或引号,例如以下CSV字符串是无效的:
var invalid1 = "one, that's me!, escaped \, comma"
这并不是真正的限制,因为任何子字符串都可以表示为单引号或双引号的值。请注意,此解决方案仅代表“逗号分隔值”的一种可能定义。
编辑:2014-05-19:添加免责声明。 编辑:2014-12-01:将免责声明移到顶部。
re_valid
中出现5次,在re_value
中出现3次)。话虽如此,可以通过使用RegExp()
构造函数而不是/RegExp字面量/
来构建正则表达式来完成这项工作。 - ridgerunnercsv-parse/lib/sync
来实现我想要的功能(即拆分格式为CSV的行)。 - Zach Smithnull
,因为它们使用 "
作为引号,而某些值包含单引号 '
。它还没有考虑到换行和行。我知道我可以扩展代码以包括这些用例,但那将导致一些笨重的大表达式,这变得极其耗时。此页面上@Bachor的答案不使用正则表达式,更易于维护和速度快两倍。 - BdR这并不能解决问题中的字符串,因为它的格式不符合RFC 4180;可接受的编码方式是用双引号来转义双引号。下面的解决方案可以正确地处理从Google电子表格下载的CSV文件。
解析单行将是错误的。根据RFC 4180,字段可能包含CRLF,这将导致任何行读取器中断CSV文件。下面是一个更新的版本,用于解析CSV字符串:
'use strict';
function csvToArray(text) {
let p = '', row = [''], ret = [row], i = 0, r = 0, s = !0, l;
for (l of text) {
if ('"' === l) {
if (s && l === p) row[i] += l;
s = !s;
} else if (',' === l && s) l = row[++i] = '';
else if ('\n' === l && s) {
if ('\r' === p) row[i] = row[i].slice(0, -1);
row = ret[++r] = [l = '']; i = 0;
} else row[i] += l;
p = l;
}
return ret;
};
let test = '"one","two with escaped """" double quotes""","three, with, commas",four with no quotes,"five with CRLF\r\n"\r\n"2nd line one","two with escaped """" double quotes""","three, with, commas",four with no quotes,"five with CRLF\r\n"';
console.log(csvToArray(test));
(单行解决方案)
function CSVtoArray(text) {
let ret = [''], i = 0, p = '', s = true;
for (let l in text) {
l = text[l];
if ('"' === l) {
s = !s;
if ('"' === p) {
ret[i] += '"';
l = '-';
} else if ('' === p)
l = '-';
} else if (s && ',' === l)
l = ret[++i] = '';
else
ret[i] += l;
p = l;
}
return ret;
}
let test = '"one","two with escaped """" double quotes""","three, with, commas",four with no quotes,five for fun';
console.log(CSVtoArray(test));
为了好玩,这里是如何从数组创建CSV的方法:
function arrayToCSV(row) {
for (let i in row) {
row[i] = row[i].replace(/"/g, '""');
}
return '"' + row.join('","') + '"';
}
let row = [
"one",
"two with escaped \" double quote",
"three, with, commas",
"four with no quotes (now has)",
"five for fun"
];
let text = arrayToCSV(row);
console.log(text);
我喜欢FakeRainBrigand的答案,但它存在一些问题:无法处理引号和逗号之间的空格,也不支持两个连续的逗号。我尝试编辑他的答案,但是我的修改被审核人员拒绝了,他们显然没有理解我的代码。这是我对FakeRainBrigand代码的版本。 还有一个演示:http://jsfiddle.net/xTezm/46/
String.prototype.splitCSV = function() {
var matches = this.match(/(\s*"[^"]+"\s*|\s*[^,]+|,)(?=,|$)/g);
for (var n = 0; n < matches.length; ++n) {
matches[n] = matches[n].trim();
if (matches[n] == ',') matches[n] = '';
}
if (this[0] == ',') matches.unshift("");
return matches;
}
var string = ',"string, duppi, du" , 23 ,,, "string, duppi, du",dup,"", , lala';
var parsed = string.splitCSV();
alert(parsed.join('|'));
人们似乎反对在这种情况下使用正则表达式。为什么?
(\s*'[^']+'|\s*[^,]+)(?=,|$)
这是代码。我还制作了一个 fiddle。
String.prototype.splitCSV = function(sep) {
var regex = /(\s*'[^']+'|\s*[^,]+)(?=,|$)/g;
return matches = this.match(regex);
}
var string = "'string, duppi, du', 23, 'string, duppi, du', lala";
console.log( string.splitCSV() );
.as-console-wrapper { max-height: 100% !important; top: 0; }
我有一个非常具体的用例,需要将Google Sheets中的单元格复制到我的Web应用程序中。这些单元格可能包括双引号和换行符。使用复制和粘贴,单元格由制表符分隔,并且具有奇怪数据的单元格被双引号引用。我尝试了主要解决方案,即使用regexp、Jquery-CSV和CSVToArray的链接文章。而http://papaparse.com/是唯一一个可以直接使用的解决方案。使用默认的自动检测选项,与Google Sheets进行复制和粘贴非常顺畅。
PEG(.js)语法,处理RFC 4180示例的内容在http://en.wikipedia.org/wiki/Comma-separated_values:
start
= [\n\r]* first:line rest:([\n\r]+ data:line { return data; })* [\n\r]* { rest.unshift(first); return rest; }
line
= first:field rest:("," text:field { return text; })*
& { return !!first || rest.length; } // ignore blank lines
{ rest.unshift(first); return rest; }
field
= '"' text:char* '"' { return text.join(''); }
/ text:[^\n\r,]* { return text.join(''); }
char
= '"' '"' { return '"'; }
/ [^"]
请在http://jsfiddle.net/knvzk/10 或https://pegjs.org/online 进行测试。
下载生成的解析器,请访问https://gist.github.com/3362830。
我已经多次使用正则表达式,但每次都需要重新学习,这很令人沮丧 :-)
所以这里提供一个非正则表达式的解决方案:
function csvRowToArray(row, delimiter = ',', quoteChar = '"'){
let nStart = 0, nEnd = 0, a=[], nRowLen=row.length, bQuotedValue;
while (nStart <= nRowLen) {
bQuotedValue = (row.charAt(nStart) === quoteChar);
if (bQuotedValue) {
nStart++;
nEnd = row.indexOf(quoteChar + delimiter, nStart)
} else {
nEnd = row.indexOf(delimiter, nStart)
}
if (nEnd < 0) nEnd = nRowLen;
a.push(row.substring(nStart,nEnd));
nStart = nEnd + delimiter.length + (bQuotedValue ? 1 : 0)
}
return a;
}
工作原理:
row
。nEnd
设置为闭合引号。nEnd
设置为下一个定界符。nStart
设置为nEnd
加上分隔符的长度。有时编写自己的小函数,而不是使用库,是很好的选择。自己的代码性能会很好,并且足迹很小。此外,您可以轻松地调整它以满足自己的需要。
if (nEnd < 0 ) { if (bQuotedValue == true) {nEnd = nRowLen - 1} else {nEnd = nRowLen} }
替换了if (nEnd < 0) nEnd = nRowLen
,以适应以引号结尾的行。希望这样说得通。 - Blueraaga正则表达式来拯救!这几行代码可以根据RFC 4180标准,正确处理带有嵌入逗号、引号和换行符的引号字段。
function parseCsv(data, fieldSep, newLine) {
fieldSep = fieldSep || ',';
newLine = newLine || '\n';
const nSep = '\x1D'; const nSepRe = new RegExp(nSep, 'g');
const qSep = '\x1E'; const qSepRe = new RegExp(qSep, 'g');
const cSep = '\x1F'; const cSepRe = new RegExp(cSep, 'g');
const fieldRe = new RegExp('(^|[' + fieldSep + '\\n])"([^"]*(?:""[^"]*)*)"(?=($|[' + fieldSep + '\\n]))', 'g');
return data
.replace(/\r/g, '')
.replace(/\n+$/, '')
.replace(fieldRe, (match, p1, p2) => {
return p1 + p2.replace(/\n/g, nSep).replace(/""/g, qSep).replace(/,/g, cSep)
})
.split(/\n/)
.map(line => {
return line
.split(fieldSep)
.map(cell => cell.replace(nSepRe, newLine).replace(qSepRe, '"').replace(cSepRe, ','))
});
}
const csv = 'A1,B1,C1\n"A ""2""","B, 2","C\n2"';
const separator = ','; // field separator, default: ','
const newline = ' <br /> '; // newline representation in case a field contains newlines, default: '\n'
let grid = parseCsv(csv, separator, newline);
// expected: [ [ 'A1', 'B1', 'C1' ], [ 'A "2"', 'B, 2', 'C <br /> 2' ] ]
注意:
\t
表示TSV(制表符分隔值)<br/>
parseCsv
函数避免了负/正向后顾,例如在Safari浏览器上也可以工作。除非另有说明,否则您不需要有限状态机。正则表达式通过使用临时替换/恢复、捕获组和正向前瞻的函数式编程方法正确处理RFC 4180。
克隆/下载代码https://github.com/peterthoeny/parse-csv-js
了解更多关于正则表达式的知识:https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex
(这在Safari浏览器上无法工作)
function parseCsv(data, fieldSep, newLine) {
fieldSep = fieldSep || ',';
newLine = newLine || '\n';
var nSep = '\x1D';
var qSep = '\x1E';
var cSep = '\x1F';
var nSepRe = new RegExp(nSep, 'g');
var qSepRe = new RegExp(qSep, 'g');
var cSepRe = new RegExp(cSep, 'g');
var fieldRe = new RegExp('(?<=(^|[' + fieldSep + '\\n]))"(|[\\s\\S]+?(?<![^"]"))"(?=($|[' + fieldSep + '\\n]))', 'g');
var grid = [];
data.replace(/\r/g, '').replace(/\n+$/, '').replace(fieldRe, function(match, p1, p2) {
return p2.replace(/\n/g, nSep).replace(/""/g, qSep).replace(/,/g, cSep);
}).split(/\n/).forEach(function(line) {
var row = line.split(fieldSep).map(function(cell) {
return cell.replace(nSepRe, newLine).replace(qSepRe, '"').replace(cSepRe, ',');
});
grid.push(row);
});
return grid;
}
const csv = 'A1,B1,C1\n"A ""2""","B, 2","C\n2"';
const separator = ','; // field separator, default: ','
const newline = ' <br /> '; // newline representation in case a field contains newlines, default: '\n'
var grid = parseCsv(csv, separator, newline);
// expected: [ [ 'A1', 'B1', 'C1' ], [ 'A "2"', 'B, 2', 'C <br /> 2' ] ]
再增加一个选项,因为我发现以上所有方法都不够简单。这个方法使用正则表达式来查找逗号或换行符,同时可以跳过带引号的项目。希望这对新手来说是容易理解的。 splitFinder
正则表达式有三个功能(用 |
分隔):
,
- 查找逗号\r?\n
- 查找换行符(如果导出程序做得好可能还会包括回车符)"(\\"|[^"])*?"
- 跳过带引号的任何内容,因为逗号和换行符在引号中无关紧要。如果引号内有转义引号 \\"
,它会在找到结束引号之前被捕获。const splitFinder = /,|\r?\n|"(\\"|[^"])*?"/g;
function csvTo2dArray(parseMe) {
let currentRow = [];
const rowsOut = [currentRow];
let lastIndex = splitFinder.lastIndex = 0;
// add text from lastIndex to before a found newline or comma
const pushCell = (endIndex) => {
endIndex = endIndex || parseMe.length;
const addMe = parseMe.substring(lastIndex, endIndex);
// remove quotes around the item
currentRow.push(addMe.replace(/^"|"$/g, ""));
lastIndex = splitFinder.lastIndex;
}
let regexResp;
// for each regexp match (either comma, newline, or quoted item)
while (regexResp = splitFinder.exec(parseMe)) {
const split = regexResp[0];
// if it's not a quote capture, add an item to the current row
// (quote captures will be pushed by the newline or comma following)
if (split.startsWith(`"`) === false) {
const splitStartIndex = splitFinder.lastIndex - split.length;
pushCell(splitStartIndex);
// then start a new row if newline
const isNewLine = /^\r?\n$/.test(split);
if (isNewLine) { rowsOut.push(currentRow = []); }
}
}
// make sure to add the trailing text (no commas or newlines after)
pushCell();
return rowsOut;
}
const rawCsv = `a,b,c\n"test\r\n","comma, test","\r\n",",",\nsecond,row,ends,with,empty\n"quote\"test"`
const rows = csvTo2dArray(rawCsv);
console.log(rows);
Id, Name, Age
1, John Smith, 65
2, Jane Doe, 30
如何根据我指定的列进行解析? - bluePearl[{Id: 1, Name: "John Smith", Age: 65}, {Id: 2, Name: "Jane Doe", Age: 30}]
。 - Seph Reed不使用正则表达式,易读,并遵循https://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules的基本规则:
function csv2arr(str: string) {
let line = ["",];
const ret = [line,];
let quote = false;
for (let i = 0; i < str.length; i++) {
const cur = str[i];
const next = str[i + 1];
if (!quote) {
const cellIsEmpty = line[line.length - 1].length === 0;
if (cur === '"' && cellIsEmpty) quote = true;
else if (cur === ",") line.push("");
else if (cur === "\r" && next === "\n") { line = ["",]; ret.push(line); i++; }
else if (cur === "\n" || cur === "\r") { line = ["",]; ret.push(line); }
else line[line.length - 1] += cur;
} else {
if (cur === '"' && next === '"') { line[line.length - 1] += cur; i++; }
else if (cur === '"') quote = false;
else line[line.length - 1] += cur;
}
}
return ret;
}
var string = "'du',23,,,lala"
- ridgerunner