I'd like to split an input string on the first colon that still has characters after it on the same line.
For this, I am using the regular expression /:(.+)/
So given the string
aaa:
bbb:ccc
I'd expect an output of
["aaa:
bbb", "ccc"]
And given the string
aaa:bbb:ccc
I'd expect an output of
["aaa", "bbb:ccc"]
Yet when I actually run these commands, I get
["aaa:
bbb", "ccc", ""]
["aaa", "bbb:ccc", ""]
As output.
So somehow, javascript is adding an empty string to the end of the array.
I have checked the documentation for String.split
and whilst it does mention that if you perform string.split
on an empty string with a specified separator, you'll get an array with 1 empty string in it (and not empty array). It makes no mention of there always being an empty string in the output, or a warning that you may get this result if you make a common mistake or something.
I'd understand if my input string had a colon at the end or something like that; then it splits at the colon and the rest of the match is empty string. That's the issue mentioned in Splitting string with regular expression to make it array without empty element - but I don't have this issue, as my input string does not end with my separator.
I know a quick solution in my case will be to just simply limit the amount of matches, via "aaa:bbb:ccc".split(/:(.+)/, 2)
, but I'm still curious:
Why does this string.split
call return an array ending with an empty string?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…