views:

60

answers:

2

In JavaScript, if I run the following code:

<script type="text/javascript">
var nameStr = 'Chris Kate Steve';
var names = nameStr.split(/[ ]/);
var names2 = nameStr.split(' ');

for (var i in names)
{
    alert(i);
}

for (var i in names2)
{
    alert(i);
}
</script>

It will alert:

0

1

2

index

input

For the first set and:

0

1

2

For the second set.

Any idea why this is?

+4  A: 
Mewp
Yeah... I was just confused by:http://www.w3schools.com/js/js_loop_for_in.asp
John
+1  A: 

split method with a string as input returns an array of substrings. So the returned Array object just has its elements as its contents.

split method with a regex as input returns an Array object that has substrings at its numerical index positions and the input string and the index of first match stored at their respective properties (just like the return value of string.match() / regex.exec() function) - hence the input and index properties.

Amarghosh