javascript regex split 产生太多项目 [英] javascript regex split produces too many items
问题描述
我正在尝试使用逗号或空格分割字符串.逗号可以选择性地前面和/或后面跟空格,空格本身也算作分隔符.代码如下所示:
I'm trying to split a string using either commas or whitespace. A comma can optionally be preceded and/or followed by whitespace, and whitespace by itself also counts as a delimiter. The code looks like this:
var answers= s.split(/(s*,s*)|s+/);
如果 s
包含字符串 'a b,c',我会得到一个包含五个项目的列表(数组),而不是预期的三个:
If s
contains the string 'a b,c', I get a list (array) containing five items instead of the expected three:
0:a, 1:undefined, 2:b, 3:,, 4:c
0:a, 1:undefined, 2:b, 3:,, 4:c
任何关于我做错了什么的建议将不胜感激.
Any advice as to what I'm doing wrong will be appreciated.
菲利普
推荐答案
That's because split
does also push capturing groups to the result array:
如果separator是一个包含捕获括号的正则表达式,那么每次separator匹配时,捕获括号的结果(包括任何未定义的结果)都会拼接到输出数组中.
If separator is a regular expression that contains capturing parentheses, then each time separator is matched the results (including any undefined results) of the capturing parentheses are spliced into the output array.
a
和 b
之间的空格与空格匹配,因此捕获组未定义.b
和 c
之间的逗号被组匹配,所以它成为你数组的第四项.
The space between a
and b
was matched by the whitespace, so the capturing group was undefined. The comma between b
and c
was matched by the group, so it became the fourth item of your array.
要解决这个问题,只需删除捕获组:
To solve the issue, just remove the capturing group:
var answers = s.split(/s*,s*|s+/);
如果你有一个更复杂的表达式需要分组,你可以像这样让它不捕获:
If you had a more complex expression where you needed grouping, you could make it non-capturing like this:
var answers = s.split(/(?:s*,s*)|s+/);
这篇关于javascript regex split 产生太多项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!