javascript 正则表达式 split 产生太多项目

发布于 2025-01-10 08:42:03 字数 293 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮萍、无处依 2025-01-17 08:42:03

这是因为 split< /a> 也会将 捕获组 推送到结果数组:

如果分隔符是包含捕获括号的正则表达式,则每次匹配分隔符时,捕获括号的结果(包括任何未定义的结果)都会拼接到输出数组中。

ab 之间的空格与空格匹配,因此捕获组未定义。 bc 之间的逗号与组匹配,因此它成为数组的第四项。

要解决这个问题,只需删除捕获组:

var answers = s.split(/\s*,\s*|\s+/);

如果您有一个需要分组的更复杂的表达式,您可以将其设置为非捕获,如下所示:

var answers = s.split(/(?:\s*,\s*)|\s+/);

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

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.

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+/);
清风不识月 2025-01-17 08:42:03

捕获组的内容将添加到结果数组中。来自 MDN 文档

如果separator是包含捕获括号的正则表达式,则每次匹配分隔符时,捕获括号的结果(包括任何未定义的结果)都会拼接到输出数组中。但是,并非所有浏览器都支持此功能。

使用非捕获组:

/(?:\s*,\s*)|\s+/

The content of capturing groups are added to the result array. From the MDN documentation:

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. However, not all browsers support this capability.

Use non-capturing groups:

/(?:\s*,\s*)|\s+/
星星的轨迹 2025-01-17 08:42:03

如果你简单地删除括号,它就会起作用:

var s = 'a,b,c'
var answers = s.split(/\s*,\s*|\s+/);
// [ 'a', 'b', 'c' ]

If you simply remove the parentheses, it will work:

var s = 'a,b,c'
var answers = s.split(/\s*,\s*|\s+/);
// [ 'a', 'b', 'c' ]
厌味 2025-01-17 08:42:03

使用正则表达式,捕获表达式 (x) 会记住匹配(并可能将其返回到 String.split)。您应该使用(非捕获)分组表达式 (?:x)。有关更多信息,请参阅 Mozilla Docs on RegExp

With regexes the capture expression (x) remembers the match (and possibly returns that to the String.split). You should use the (non-capturing) grouping expression (?:x). See e.g. the Mozilla Docs on RegExp for more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文