修剪nodejs crypto返回的字符串中的非ascii字符

时间:2022-06-12 15:25:46

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

我已经使用nodejs加密库成功解密了敏感数据。

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

问题是解密的数据有一个尾随的非ascii字符。

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

2 个解决方案

#1


5  

I think following would be suffice.

我认为以下就足够了。

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

#2


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:

这应该删除字符串末尾的所有空格和非ascii字符,但将它们留在中间,例如:

"Abcde ffאggg ג ב".fulltrim();
// "Abcde ffאggg";

#1


5  

I think following would be suffice.

我认为以下就足够了。

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

#2


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:

这应该删除字符串末尾的所有空格和非ascii字符,但将它们留在中间,例如:

"Abcde ffאggg ג ב".fulltrim();
// "Abcde ffאggg";