The power of String.prototype.split() … almost
Actually think the “time spent wishing a slow painful death on Bill Gates” segment needs expanding – Bill isn’t directly to blame. In fact it would be great if the IE team could be more forthcoming and put names to features, so we know exactly who to swear at: “Hi, I’m [insert name] and I’m the guy that put an undefined value at end of your array, every time you leave that trailing comma, resulting in bugs that will keep you amused for hours :)”.
From the spec (p103 / 104);
If separator is a regular expression that contains capturing parentheses, then each time separator is
matched the results (including any undefined results) of the capturing parentheses are spliced into the
output array. […]
For example Perl…
use Data::Dumper; print Dumper(split(/(:)/, 'a:b:c'));
$VAR1 = 'a'; $VAR2 = ':'; $VAR3 = 'b'; $VAR4 = ':'; $VAR5 = 'c';
print_r(preg_split('/(:)/', 'a:b:c', -1, PREG_SPLIT_DELIM_CAPTURE));
Array (  => a  => :  => b  => :  => c )
import re print re.compile('(:)').split('a:b:c')
['a', ':', 'b', ':', 'c']
alert( "a:b:c".split(/(:)/) );
…which in Firefox (with help from Firebug) gives me;
Likewise Opera 9 does the right this. But in IE (6)…
Where I my captured seperators!.
As Simon put it;
Why is this a big deal? Because it suddenly makes writing simple parsers and tokenisers a whole heck of a lot easier.
Actually blaming the IE Team is probably unfair – this seems to be a “feature” delivered by the JScript team and appears to have crept into JScript.NET as well, for example with a script like
import System.Windows.Forms; MessageBox.Show("a:b:c".split(/(:)/));
I can compile it with the jsc compiler in DOS like
D:js> C:WINDOWSMicrosoft.NETFrameworkv2.0.50727jsc.exe /nologo split.js then run the output
split.exe to get exactly the same –