Wednesday, January 7th, 2009
ActionScript Array.split() and RegExp

I'm creating a function that accepts the contents of an XML node as an argument (or is it a parameter? I'm never sure...) and splits it into an array. Not rocket science.

Here's the thing, though: Because I can't guarantee that people editing or creating the XML files with which these functions are intended to work will enter things correctly, I'm trying to make them as...accepting as possible; for example, if someone were to use the following:

 
<panel onExit="prev   : p1,     next:      p4">...</panel>

bad things would happen if I don't allow for such an arbitrary placement of commas and colons.

I initially tried cleaning it up like this:

myArr=panel.@onExit.toString().split(/(\W*),(\W*)/i);

which returns something like...well, something unusable. According to my handy-dandy little regular expression thingy (RegEx-Buddy), such an expression should split the above pseudo-XML quite nicely. In practice, however, it doesn't.

So I recreated all of the above in Javascript -- and experienced the same result.

Oddly enough, if you do the following:

myStr=panel.@onExit.toString().replace(/(\W*),(\W*)/i, ",");
myArr=myStr.split(",");

everything looks like it should. I really would have preferred the one-line method, since I'm lazy and not a fan of having to write extra lines when I don't think I should have to, but since it doesn't work (for me, anyway), the two-line method's not terrible.

Just thought I'd throw that out there.

Be the first to comment!

Leave a reply