4

I have successfully decrypted a sensitive data using nodejs crypto library.

The problem is that the decrypted data has a trailing non-ascii characters.

How do I trim that?

My current trim function I below does not do the job.

String.prototype.fulltrim = function () {
  return this.replace( /(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '' ).replace( /\s+/g, ' ' );
};
Inkbug
  • 1,682
  • 1
  • 10
  • 26
ian
  • 1,505
  • 2
  • 17
  • 24

2 Answers2

5

I think following would be suffice.

str.replace(/[^A-Za-z 0-9 \.,\?""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]*/g, '') ; 
Horen
  • 11,184
  • 11
  • 71
  • 113
Anand
  • 14,545
  • 8
  • 32
  • 44
0

Based on this answer, you can use:

String.prototype.fulltrim = function () {
  return this.replace( /([^\x00-\xFF]|\s)*$/g, '' );
};

This should remove all spaces and non-ascii characters at the end of the string, but leave them in the middle, for example:

"Abcde ffאggg ג ב".fulltrim();
// "Abcde ffאggg";
Community
  • 1
  • 1
Inkbug
  • 1,682
  • 1
  • 10
  • 26