0

I want to replace the characters while typing in the Textarea. But with the following code snippets (nearly 250 lines are there), here I'm using replace() method of RegEx. It checks against the replacement too. I want it should check only the pattern/regexp.

Test

m -> A //ok
M -> B //ok
, -> F //Wrong, should be C 

How can I get hte replacement without changing the order of hte line?

<!DOCTYPE html PUBLIC "-//W3C//Dtd HTML 4.0 transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <meta charset="UTF-8">
     
   </head>
   <body onload="javascript:formTest.source.focus()">
      <script language="javascript" type="text/javascript">
         var text;
         
         function startConvert()
         {
         text = document.formTest.source.value;  
            
   text = text.replace(/\u006d/g, "A");
   text = text.replace(/M/g, "B");
   text = text.replace(/,/g, "C");
   text = text.replace(/</g, "D");
   text = text.replace(/c/g, "E");
   text = text.replace(/C/g, "F");
   text = text.replace(/v/g, "G");
   text = text.replace(/V/g, "H");
   text = text.replace(/I/g, "I");
   text = text.replace(/x/g, "J");
   text = text.replace(/X/g, "K");
   text = text.replace(/xs/g, "L");
   
   text = text.replace(/\//g, "@");
                    
         document.formTest.destination.value=text;
         }
         
            
      </script>
      <center>
         <table width="500" >
            <tbody>
               <tr>
                  <form name="formTest">
                     <td>        
      <textarea onkeyup="startConvert();" 
      onclick="startConvert();" 
      name="source" 
      rows="5" 
      onselect="startConvert();">
      </textarea>
     
                     </td>
                     <td>
                        <textarea 
      name="destination" rows="5"></textarea>
                     </td>
                  </form>
               </tr>
            </tbody>
         </table>
      </center>
   </body>
</html>
test team
  • 677
  • 3
  • 12
  • 27

3 Answers3

2

If a substring from an earlier .replace replaced with something that's being searched for in a later .replace, the result of the earlier .replace will be overwritten by the later one. I'd use an object of replacement values instead, with only a single .replace, so that no character gets checked and replaced more than once.

If the characters to match may be characters with a special meaning in a regex, like [, escape them first:

var text;
const replacements = {
  '\u006d': 'A',
  M: 'B',
  ',': 'C',
  '<': 'D',
  c: 'E',
  C: 'F',
  v: 'G',
  V: 'H',
  I: 'I',
  x: 'J',
  X: 'K',
  xs: 'L',
};
const pattern = new RegExp(
  Object.keys(replacements)
    .map(s => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'))
    .join('|'),
  'g'
);
function startConvert() {
  document.formTest.destination.value = document.formTest.source.value
    .replace(pattern, match => replacements[match]);
}
<center>
  <table width="500">
    <tbody>
      <tr>
        <form name="formTest">
          <td>
            <textarea onkeyup="startConvert();" onclick="startConvert();" name="source" rows="5" onselect="startConvert();">
      </textarea>

          </td>
          <td>
            <textarea name="destination" rows="5"></textarea>
          </td>
        </form>
      </tr>
    </tbody>
  </table>
</center>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Instead of checking against the whole string,

var str = document.formTest.source.value; 
var text = str .substr(str .length - 1);
text = text.replace(/\u006d/g, "A");
...

This will do.

David
  • 15,894
  • 22
  • 55
  • 66
-2

use onkeyup="functionName()", remove onclick() and onselect()

Mohit
  • 80
  • 7