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?
splitmethod with a string as input returns an array of substrings. So the returnedArrayobject just has its elements as its contents.splitmethod with a regex as input returns anArrayobject 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 ofstring.match()/regex.exec()function) – hence theinputandindexproperties.