0

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

1
  • how are we to tell how many digits the last element of the array should be. Commented Nov 16, 2009 at 18:54

2 Answers 2

3
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.
                        */
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Put it as an answer and tick it so other people can learn from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.