Solved

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

Posted on 2011-03-18
3
228 Views
Last Modified: 2012-05-11
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');
0
Comment
Question by:saturation
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35168996
Use : sTime.replace(/\d/g, function($1) { return [6,7,8,9,0,1,2,3,4,5][$1]; })
<html><head><script>

sTime = "00112";
alert(  sTime.replace(/\d/g, function($1) { return [6,7,8,9,0,1,2,3,4,5][$1]; })  );

</script><body></body></html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35169018
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

0
 

Author Closing Comment

by:saturation
ID: 35169049
Great...Now I have another similar question related to this.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question