javascript - trim non-ascii characters from string returned by nodejs crypto - Stack Overflow

I have successfully decrypted a sensitive data using nodejs crypto library.The problem is that the decr

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744285240a4566766.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信