admin管理员组文章数量:1277896
While learning JavaScript, I did not get why the output when we print the array returned of the Sting.split() method (with regular expression as an argument) is as explained below.
var colorString = "red,blue,green,yellow";
var colors = colorString.split(/[^\,]+/);
document.write(colors); //this print 7 times ma: ,,,,,,,
However when I print individual element of the array colors, it prints an empty string, three mas and an empty string:
document.write(colors[0]); //empty string
document.write(colors[1]); //,
document.write(colors[2]); //,
document.write(colors[3]); //,
document.write(colors[4]); //empty string
document.write(colors[5]); //undefined
document.write(colors[6]); //undefined
Then, why printing the array directly gives seven mas.
Though I think its correct to have three mas in the second output, I did not get why there is a starting (at index 0) and ending empty string (at index 4).
Please explain I am screwed up here.
While learning JavaScript, I did not get why the output when we print the array returned of the Sting.split() method (with regular expression as an argument) is as explained below.
var colorString = "red,blue,green,yellow";
var colors = colorString.split(/[^\,]+/);
document.write(colors); //this print 7 times ma: ,,,,,,,
However when I print individual element of the array colors, it prints an empty string, three mas and an empty string:
document.write(colors[0]); //empty string
document.write(colors[1]); //,
document.write(colors[2]); //,
document.write(colors[3]); //,
document.write(colors[4]); //empty string
document.write(colors[5]); //undefined
document.write(colors[6]); //undefined
Then, why printing the array directly gives seven mas.
Though I think its correct to have three mas in the second output, I did not get why there is a starting (at index 0) and ending empty string (at index 4).
Please explain I am screwed up here.
Share Improve this question asked Oct 13, 2012 at 21:20 Mahesha999Mahesha999 24.8k30 gold badges129 silver badges210 bronze badges3 Answers
Reset to default 8/[^\,]+/
splits on one or more characters that are not a ma. Thus, JavaScript will split your string on red
, blue
etc. The resulting leftovers, then, are the empty string at the beginning (the substring from index 0 to 0), the mas, and the empty string at the end. If you go out of bounds of the array you get undefined
(as with any array).
red,blue,green,yellow
xxx xxxx xxxxx xxxxxx <-- x is what is being eaten during split, because it's the delimiter
You just want .split(",")
, which splits on mas, so that the mas are eaten and you are left with the colors.
Now, when you do document.write(someArray)
, the array is converted into a string so that it can be displayed. This effectively means someArray.join()
is called, which by default puts mas in between. So you get mas joined by mas, resulting in even more mas.
When you print out the array, the different elements of the array are also separated by mas. So your output are these 5 array elements:
[empty string],[ma],[ma],[ma],[empty string]
Amounting to 7 mas. The reason why you get mas and empty strings instead of colors is, that split
will split at everything that matches (instead of giving you back everything that matches). So simply don't use regular expressions at all, but just split at ,
:
var colors = colorString.split(',');
[^\,] <- this means anything BUT mas.
try
var colors = colorString.split(',');
本文标签: regexjavascript split() array containsStack Overflow
版权声明:本文标题:regex - javascript split() array contains - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741218191a2360458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论