views:

92

answers:

2

I have an issue with some Arabic text where I need to flip numbers inside a string. So this:

"Some text written in 1982 by someone with m0123456 or 12-to-13"

Should become:

"Some text written in 2891 by someone with m6543210 or 21-to-31"

A regex solution will be great. The more optimized for large strings the better.

Any hints?

+1  A: 
theText.replace(/\d+/g, function(s:String){ return s.split("").reverse().join(""); })

(Disclaimer: only tested for Javascript, not ActionScript.)

KennyTM
I'll try this in Flash. It might need to be tweaked to work. Fingers crossed...
Makram Saleh
Perfect! I adjusted it a little bit for Flash (see my answer)
Makram Saleh
A: 

Thanks @KennyTM! Your solution worked flawlessly (after some tweaks).

I just had to specify the type of the regexp pattern, and remove the strict argument type in line 3 (:String)

var theText = "Some text written in 1982 by someone with m0123456 or 12-to-13";
var pattern:RegExp = /\d+/g;
var result = theText.replace(pattern, function(s){ return s.split("").reverse().join("") })

trace(result);   //Some text written in 2891 by someone with m6543210 or 21-to-31
Makram Saleh
I don't understand why you had to make the changes you made... the code KennyTM provided should work perfectly for AS3 too.
jonathanasdf
The only thing that didn't work was the :String in the attribute. Just remove that and you're good to go.
Makram Saleh