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

trim non-ascii characters from string returned by nodejs crypto

本文关键字:修剪 ascii 字符 字符串 nodejs-crypto 返回      更新时间:2023-09-28

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

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

我该怎么修剪?

我下面的当前修剪功能不起作用。

String.prototype.fulltrim = function () {
  return this.replace( /(?:(?:^|'n)'s+|'s+(?:$|'n))/g, '' ).replace( /'s+/g, ' ' );
};

我认为以下内容就足够了。

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

基于这个答案,您可以使用:

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

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

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