简体   繁体   中英

Replacing non-printable ASCII character not working in work notes

I am trying to add text in the work notes which may contain non-printable ASCII characters. These characters do not get replaced as expected before storing it in the database.

<work_notes>
TEST
X  000  000  0x00  00000000  NUL  (Null char.)
  001  001  0x01  00000001  SOH  (Start of Header)
  002  002  0x02  00000010  STX  (Start of Text)
  003  003  0x03  00000011  ETX  (End of Text)
  004  004  0x04  00000100  EOT  (End of Transmission)
  005  005  0x05  00000101  ENQ  (Enquiry)
  006  006  0x06  00000110  ACK  (Acknowledgment)
  007  007  0x07  00000111  BEL  (Bell)
  008  010  0x08  00001000   BS  (Backspace)
      009  011  0x09  00001001   HT  (Horizontal Tab)

  010  012  0x0A  00001010   LF  (Line Feed)
  011  013  0x0B  00001011   VT  (Vertical Tab)
  012  014  0x0C  00001100   FF  (Form Feed)

  013  015  0x0D  00001101   CR  (Carriage Return)
  014  016  0x0E  00001110   SO  (Shift Out)
  015  017  0x0F  00001111   SI  (Shift In)
  016  020  0x10  00010000  DLE  (Data Link Escape)
  017  021  0x11  00010001  DC1  (XON)(Device Control 1)
  018  022  0x12  00010010  DC2  (Device Control 2)
  019  023  0x13  00010011  DC3  (XOFF)(Device Control 3)
  020  024  0x14  00010100  DC4  (Device Control 4)
  021  025  0x15  00010101  NAK  (Negative Acknowledgement)
  022  026  0x16  00010110  SYN  (Synchronous Idle)
  023  027  0x17  00010111  ETB  (End of Trans. Block)
  024  030  0x18  00011000  CAN  (Cancel)
  025  031  0x19  00011001   EM  (End of Medium)
  026  032  0x1A  00011010  SUB  (Substitute)
  027  033  0x1B  00011011  ESC  (Escape)
  028  034  0x1C  00011100   FS  (File Separator)
  029  035  0x1D  00011101   GS  (Group Separator)
  030  036  0x1E  00011110   RS  (Request to Send)(Record Separator)
  031  037  0x1F  00011111   US  (Unit Separator)
</work_notes>

The square shown in the work notes are actual characters but here in the textarea it is not getting displayed.

The code I have written to replace the Escape character is

/**
 * Escape a string for XML.
 * @param {String} txt
 * @return {String}
 */
ImDataHelper.escapeXml = function (txt) {
  var str = txt;
  // Replace the escape character.
  txt = str.replace(/x1B/g,''); 
  // copied from SOAPMessage script include
  return Packages.org.apache.commons.lang.StringEscapeUtils.escapeXml(txt);
};

The output of running this transaction is as follows

<work_notes>2019-04-09 13:31:37 - Shaji Kalidasan (Work Notes)
TEST
X  000  000  0x00  00000000  NUL  (Null char.)
  001  001  0x01  00000001  SOH  (Start of Header)
  002  002  0x02  00000010  STX  (Start of Text)
  003  003  0x03  00000011  ETX  (End of Text)
  004  004  0x04  00000100  EOT  (End of Transmission)
  005  005  0x05  00000101  ENQ  (Enquiry)
  006  006  0x06  00000110  ACK  (Acknowledgment)
  007  007  0x07  00000111  BEL  (Bell)
  008  010  0x08  00001000   BS  (Backspace)
      009  011  0x09  00001001   HT  (Horizontal Tab)

  010  012  0x0A  00001010   LF  (Line Feed)
  011  013  0x0B  00001011   VT  (Vertical Tab)
  012  014  0x0C  00001100   FF  (Form Feed)

  013  015  0x0D  00001101   CR  (Carriage Return)
  014  016  0x0E  00001110   SO  (Shift Out)
  015  017  0x0F  00001111   SI  (Shift In)
  016  020  0x10  00010000  DLE  (Data Link Escape)
  017  021  0x11  00010001  DC1  (XON)(Device Control 1)
  018  022  0x12  00010010  DC2  (Device Control 2)
  019  023  0x13  00010011  DC3  (XOFF)(Device Control 3)
  020  024  0x14  00010100  DC4  (Device Control 4)
  021  025  0x15  00010101  NAK  (Negative Acknowledgement)
  022  026  0x16  00010110  SYN  (Synchronous Idle)
  023  027  0x17  00010111  ETB  (End of Trans. Block)
  024  030  0x18  00011000  CAN  (Cancel)
  025  031  0x19  00011001   EM  (End of Medium)
  026  032  0x1A  00011010  SUB  (Substitute)
  027  033  0  00011011  ESC  (Escape)
  028  034  0x1C  00011100   FS  (File Separator)
  029  035  0x1D  00011101   GS  (Group Separator)
  030  036  0x1E  00011110   RS  (Request to Send)(Record Separator)
  031  037  0x1F  00011111   US  (Unit Separator)
</work_notes>

As you can see, it has replaced only 'x1B' from '0x1B' but not the actual ASCII Escape character shown in the square.

You need to escape these ASCII codes with \\ in a regex (I'm logging the string length in the example because the character won't show in console):

 var str = String.fromCharCode(27) + ' 027 033 0x1B 00011011 ESC (Escape)'; console.log('length: ', str.length); str = str.replace(/\\x1B/g, ''); console.log('length: ', str.length);

See in Regex101

NOTE: You can also use them in an interval like this: /[\\x00-\\x1F]/g

See in Regex101

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM