Solved

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

Posted on 2011-03-18
3
221 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
  • 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now