Why do match and split produce different results? This is in Actionscript 3.0, but if this is also true outside of AS3, I would like to know why for that as well.
Example:
var txt:String = "somethingorother";
var re:RegExp = /(\w{2,2})/g;
trace("\t txt.split = " + txt.split(re) + " -- " + txt.split(re).length);
trace("\t txt.match = " + txt.match(re) + " -- " + txt.match(re).length);
Results:
txt.split = ,so,,me,,th,,in,,go,,ro,,th,,er, -- 17
txt.match = so,me,th,in,go,ro,th,er -- 8
Edit:
With the conditions given, I expect the results to be an identical array (with the exception, in this case, of match not finding a final entry for odd-length Strings). Why is there the extra entries in the split? What is split finding that match is getting “correct”?
splitsplits a string by another string or by a regular expression, keeping any parenthesized groups if the argument is the latter case, which is the behavior you are seeing currently. It is not intended to have the same functionality asmatchat all, which is used to retrieve one or many matches of a regular expression on a string.splitwill split a string by your regular expression, and keep the parenthesized groups – as separate entries. Therefore, when splitting, the matches are kept separate from the string between them, which is empty – hence your result.