为什么 'abc'.split(/(a|b|c)/) 在 Javascript 中给出 a、b、c 和空字符串?
我正在尝试编写一个函数来将单词拆分为字母,但我得到了一个奇怪的结果:
'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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用全局正则表达式进行字符串匹配,而不是拆分...
这更有意义,因为您真正关心的是匹配,而不是匹配之间的内容。
如果您想匹配从 a 到 z 的任何字符,您可以这样做...
我也使其不区分大小写以匹配大写字母。
Instead of splitting, you can do a string match with a global regex...
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...
I made it case insensitive to match upper case too.
分隔符之间有空字符串,因为您要在这些字母上进行拆分。
a
、b
和c
是“分隔符”标记,''
是实际的“单词”你介于两者之间。例如,如果您要在
','
上拆分"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
, andc
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".请参阅此页面:
如果删除括号,您将得到预期的结果:
也就是说,您将在存在“a”、“b”或“c”的地方拆分字符串,只保留其间的空格。
但听起来这不是你想要的。如果您想要结果 [“a”,“b”,“c”],请使用“match”,正如下面有人建议的那样。
See this page:
If you remove the parentheses, you'll get the expected result:
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.
其他答案已经涵盖了正则表达式的角度,但是
对于您既定的目标,您根本不需要正则表达式。只需将空字符串传递给
.split()
:结果将是:
Other answers have covered the regular expression angle, but
For your stated goal you don't need a regular expression at all. Just pass an empty string to
.split()
:The result will be: