Panzercrisis
Panzercrisis

Reputation: 4750

How can you split a String on all whitespace with a regular expression in AS3?

Please Note: I am fully aware that this and other questions similar to it have been asked a million times, but as I have searched the Internet, I have pretty much always run into two problems in these cases:

  1. The question is something a little different, like purging all whitespace, as opposed to splitting on it.

  2. The answer given, even when accepted, is wrong. I don't understand why this point is true so much of the time here, and I agree that something's strange about this, but it is. I have tried people's "accepted" answers, on multiple occasions, and found that they have simply not been debugged at all.

So here's my question: I want to be able to take a String in AS3 and split it into an array on any and every whitespace character. Just like:

var arry:Array = ("This is a string.").split(" ");

except that I want to essentially use a wildcard character that encompasses any whitespace character. I want to use a regular expression to avoid excessive function calls and stuff like that.

How can this be done? Thanks.

Upvotes: 0

Views: 1149

Answers (1)

mfa
mfa

Reputation: 5087

String.split allows for a regular expression delimiter. '\s' is for whitespace. The examples below produce the same array output.

var arry = ("This is a string.").split(/\s/);
var arry = ("This is\ta\nstring.").split(/\s/);

Upvotes: 2

Related Questions