Link to home
Start Free TrialLog in
Avatar of saturation
saturation

asked on

Javascript replace...Need to replace all at once.

I have a string of numbers called "sTime".   The problem I'm having is that it's replacing my numbers in a sequential order, so if I have a 0 that gets replaced by a 6, when it goes down another 6 rows of code, it's replacing the 6 with a 2.   I only want it to replace it once, even though that number may appear more than once in the string.  For example,

00112 needs to be 66778 rather than 22334, which is wrong.   Help?   My code is below.

            sTime = sTime.replace(/0/g,'6');
            sTime = sTime.replace(/1/g,'7');
            sTime = sTime.replace(/2/g,'8');
            sTime = sTime.replace(/3/g,'9');
            sTime = sTime.replace(/4/g,'0');
            sTime = sTime.replace(/5/g,'1');
            sTime = sTime.replace(/6/g,'2');
            sTime = sTime.replace(/7/g,'3');
            sTime = sTime.replace(/8/g,'4');
            sTime = sTime.replace(/9/g,'5');
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
work too :
sTime = sTime.replace(/\d/g, function($1) { return (-$1-6) * -1; });

Open in new window


or again :
sTime = sTime.replace(/\d/g, function($1) { return Math.abs(-$1-6); });

Open in new window

Avatar of saturation
saturation

ASKER

Great...Now I have another similar question related to this.