【问题标题】:trim non-ascii characters from string returned by nodejs crypto从 nodejs crypto 返回的字符串中修剪非 ascii 字符
【发布时间】:2012-07-14 22:36:16
【问题描述】:

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

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

如何修剪?

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

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

【问题讨论】:

    标签: javascript string node.js cryptography trim


    【解决方案1】:

    我认为以下就足够了。

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

    【讨论】:

      【解决方案2】:

      基于this answer,你可以使用:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 2012-11-05
        相关资源
        最近更新 更多