为什么 'abc'.split(/(a|b|c)/) 在 Javascript 中给出 a、b、c 和空字符串?

发布于 2025-01-07 07:04:57 字数 390 浏览 1 评论 0原文

我正在尝试编写一个函数来将单词拆分为字母,但我得到了一个奇怪的结果:

'abc'.split(/(a|b|c)/) 

给出:

["", "a", "", "b", "", "c", ""]

顺便说​​一句,我在Python中看到了相同的结果,所以显然问题是我!

>>> re.split( '(a|b|c)', 'abc')
['', 'a', '', 'b', '', 'c', '']

问题是,为什么字母之间会插入空字符串呢?我预计

["a", "b", "c"]

谢谢!

I’m trying to write a function to split a word into letters, and I’m getting a weird result:

'abc'.split(/(a|b|c)/) 

Gives:

["", "a", "", "b", "", "c", ""]

Incidentally, I see the same results in Python, so clearly the problem is me!

>>> re.split( '(a|b|c)', 'abc')
['', 'a', '', 'b', '', 'c', '']

The problem is, why are there empty strings intercalated between the letters? I expected

["a", "b", "c"]

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

树深时见影 2025-01-14 07:04:57

您可以使用全局正则表达式进行字符串匹配,而不是拆分...

'abc'.match(/(a|b|c)/g); // ["a", "b", "c"]

这更有意义,因为您真正关心的是匹配,而不是匹配之间的内容。

如果您想匹配从 a 到 z 的任何字符,您可以这样做...

'abc'.match(/([a-z])/gi);

我也使其不区分大小写以匹配大写字母。

Instead of splitting, you can do a string match with a global regex...

'abc'.match(/(a|b|c)/g); // ["a", "b", "c"]

This makes more sense since all you really care about is the match, not what's between the match.

If you wanted to match any character from a to z, you can do this...

'abc'.match(/([a-z])/gi);

I made it case insensitive to match upper case too.

酒中人 2025-01-14 07:04:57

分隔符之间有空字符串,因为您要在这些字母上进行拆分。 abc 是“分隔符”标记,'' 是实际的“单词”你介于两者之间。

例如,如果您要在 ',' 上拆分 "x,y",您会期望返回 "x"、"," 和 "y ”。如果将 ",y" 拆分为 ',',则会返回“”、“,”和“y”。因此,如果您在 'a' 上拆分 "ay",则输出为 ""、"a" 和 "y"。

There are empty strings between your delimiters because you're splitting on those letters. a, b, and c are the "delimiter" tokens, and '' is the actual "word" you've got in between.

For example, if you were to split "x,y" on ',', you'd expect to get back "x", ",", and "y". If you split ",y" on ',', you get back "", ",", and "y". So if you split "ay" on 'a', your output is "", "a", and "y".

傲鸠 2025-01-14 07:04:57

请参阅此页面

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

如果删除括号,您将得到预期的结果:

> 'abc'.split(/a|b|c/)
["", "", "", ""]

也就是说,您将在存在“a”、“b”或“c”的地方拆分字符串,只保留其间的空格。

但听起来这不是你想要的。如果您想要结果 [“a”,“b”,“c”],请使用“match”,正如下面有人建议的那样。

See this page:

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.

If you remove the parentheses, you'll get the expected result:

> 'abc'.split(/a|b|c/)
["", "", "", ""]

That is, you'll split your string wherever there's an "a", "b", or "c", leaving only the spaces in between.

But it sounds like that's not what you want. If you want the result ["a", "b", "c"], use "match", as someone suggested below.

尹雨沫 2025-01-14 07:04:57

其他答案已经涵盖了正则表达式的角度,但是

我正在尝试编写一个函数来将单词拆分为字母

对于您既定的目标,您根本不需要正则表达式。只需将空字符串传递给 .split()

'abc'.split('')

结果将是:

['a','b','c']

Other answers have covered the regular expression angle, but

I’m trying to write a function to split a word into letters

For your stated goal you don't need a regular expression at all. Just pass an empty string to .split():

'abc'.split('')

The result will be:

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