Replace Letters in String with Numbers

Posted on 2007-10-02
Last Modified: 2010-08-05
I need to have a simple JavaScript form on a page that a user can enter a MAC Adress and have it 'translated' to the numbers the user would push on their telephone keypad to enter the number.  Very similar to a 1-800 number when the telephone number spells out a word - 1-800-GET-BEER would be 1-800-438-2337.  In this case however, it is much simpler as there are far fewer letters.

If someone could give me the whole html page from <html> to </html> that would be awesome!

Here are the basic rules for this translation:

i.      0  9 = 0  9
ii.      A = 22
iii.      B = 222
iv.      C = 2222
v.      D = 33
vi.      E = 333
vii.      F = 3333

So if I typed into the form:  000b8200e395  I would get the number -->  0002228200333395

The 'B' was converted to '222' and the 'E' was converted to '333' and the string was put back together.

Question by:joedunn
    LVL 63

    Accepted Solution

    Here an example:


    var num = {A:22,B:222,C:2222,D:33,E:333,F:3333};


    function reNum(theStr){
      for(var r in num){
        theStr = theStr.replace(RegExp(r,"gi"),num[r]);
      return theStr;


    LVL 7

    Assisted Solution

    Give this a try:

    function convertMacAddress(macAddress){
          var txt = macAddress.toLowerCase();

          newTxt = txt.replace('a','22');
          newTxt = newTxt.replace('b','222');
          newTxt = newTxt.replace('c','2222');
          newTxt = newTxt.replace('d','33');
          newTxt = newTxt.replace('e','333');
          newTxt = newTxt.replace('f','3333');

          return newTxt;

    function convertIt(){
          var string = document.forms['mac_convert'].elements['macAddress'].value;
          mAddress = convertMacAddress(string);
          document.getElementById('result').innerHTML = mAddress;
          return false;

    <form id="mac_convert" name="mac_convert" action="" method="get">
    <input type="text" name="macAddress" id="macAddress" /><input type="submit" value="convert" onclick="return convertIt();" />
    <div id="result"></div>
    LVL 7

    Expert Comment

    Zvonko's answer is better. I was never really good at Regular expressions.
    LVL 63

    Expert Comment

    Thanks :)
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In Part 1 ( 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'…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    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…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now