Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-18
3
Medium Priority
?
230 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

670 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