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 badges
Add a ment  | 

3 Answers 3

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