ian
ian

Reputation: 1675

trim non-ascii characters from string returned by nodejs crypto

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, ' ' );
};

Upvotes: 4

Views: 7390

Answers (2)

Anand
Anand

Reputation: 14935

I think following would be suffice.

str.replace(/[^A-Za-z 0-9 \.,\?""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]*/g, '') ; 

Upvotes: 5

Inkbug
Inkbug

Reputation: 1692

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";

Upvotes: 0

Related Questions