Sergiu
Sergiu

Reputation: 1

AS3 regex split

I want to split a number using regex. I have a number like xyz (x and y are single digits, z can be a 2 or three digit number), for example 001 or 103 or 112. I want to split it into separate numbers. This can be done, if I'm not wrong by doing split("",3); This will split the number (saved as string, but I don't think it makes difference in this case) 103 in an array with values 1,0,3. Since here it's easy,the fact is that the last number z may be a 2 or 3 digit number. So I could have 1034, 0001, 1011 so on. And I have to split it respectively into [1,0,34] [0,0,01] [1,0,11] How can I do that?

Thanks

Sergiu

Upvotes: 0

Views: 1796

Answers (2)

Amarghosh
Amarghosh

Reputation: 59451

var regex:RegExp = /(\d)(\d)(\d+)/;
var n:Number = 1234;
var res:Array = regex.exec(n.toString()) as Array;
trace(res.join("\n"); /** Traces:
                        * 
                        * 1234
                        * 1
                        * 2
                        * 34
                        * 
                        * The first 1234 is the whole matched string 
                        * and the rest are the three (captured) groups.
                        */

Upvotes: 3

slacky
slacky

Reputation: 101

Found the solution, I was going the hard way...it was just possible to use substr to substract the charcaters I want and the put them in an array.

Upvotes: 0

Related Questions