javascript正则表达式拆分产生太多项目 [英] javascript regex split produces too many items

查看:132
本文介绍了javascript正则表达式拆分产生太多项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用逗号或空格分割字符串。逗号可以选择在空格之前和/或之后,空格本身也可以作为分隔符。代码如下所示:

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 包含字符串'ab, 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.

Phillip

推荐答案

那是因为 split 也推捕获组到结果数组:

That's because split does also push capturing groups to the result array:


如果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.

s 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正则表达式拆分产生太多项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆