views:

187

answers:

3

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

A: 

In poor words, it has to split the first and the second number. Everything which remains after the first 2 number or digits should be placed in the last element of the array...

Sergiu
A: 

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.

slacky
Put it as an answer and tick it so other people can learn from it.
Goose Bumper
+1  A: 
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.
                        */
Amarghosh