简体   繁体   English

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

[英]trim non-ascii characters from string returned by nodejs crypto

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

我认为跟随就足够了。

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: 这应删除字符串末尾的所有空格和非ASCII字符,但将其保留在中间,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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