admin管理员组

文章数量:1402855

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

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, ' ' );
};
Share Improve this question edited Jul 16, 2012 at 5:10 Inkbug 1,6921 gold badge11 silver badges28 bronze badges asked Jul 16, 2012 at 4:45 ianian 1,6952 gold badges19 silver badges24 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

I think following would be suffice.

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

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

本文标签: javascripttrim nonascii characters from string returned by nodejs cryptoStack Overflow