javascript 正则表达式 split 产生太多项目
我正在尝试使用逗号或空格分割字符串。逗号前面和/或后面可以有空格,并且空格本身也算作分隔符。代码如下所示:
var answers= s.split(/(\s*,\s*)|\s+/);
如果 s
包含字符串“ab,c”,我会得到一个包含五个项目而不是预期的三个项目的列表(数组):
0:a、1:未定义、2:b、3:,、4:c
任何关于我做错了什么的建议将不胜感激。
菲利普
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+/);
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
Any advice as to what I'm doing wrong will be appreciated.
Phillip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为
split
< /a> 也会将 捕获组 推送到结果数组:a
和b
之间的空格与空格匹配,因此捕获组未定义。b
和c
之间的逗号与组匹配,因此它成为数组的第四项。要解决这个问题,只需删除捕获组:
如果您有一个需要分组的更复杂的表达式,您可以将其设置为非捕获,如下所示:
That's because
split
does also push capturing groups to the result array:The space between
a
andb
was matched by the whitespace, so the capturing group was undefined. The comma betweenb
andc
was matched by the group, so it became the fourth item of your array.To solve the issue, just remove the capturing group:
If you had a more complex expression where you needed grouping, you could make it non-capturing like this:
捕获组的内容将添加到结果数组中。来自 MDN 文档:
使用非捕获组:
The content of capturing groups are added to the result array. From the MDN documentation:
Use non-capturing groups:
如果你简单地删除括号,它就会起作用:
If you simply remove the parentheses, it will work:
使用正则表达式,捕获表达式
(x)
会记住匹配(并可能将其返回到String.split
)。您应该使用(非捕获)分组表达式(?:x)
。有关更多信息,请参阅 Mozilla Docs on RegExp 。With regexes the capture expression
(x)
remembers the match (and possibly returns that to theString.split
). You should use the (non-capturing) grouping expression(?:x)
. See e.g. the Mozilla Docs on RegExp for more.