简单的ascii替换 - Javascript [英] Simple ascii replacement - Javascript

查看:87
本文介绍了简单的ascii替换 - Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个javascript函数,它接受一个字符串参数并检查低于32的ascii字符,用空字符串替换它们 - >。我是javascript的新手,所以我想知道是否有人能指出我正确的方向?

I'm looking for a javascript function which takes a string parameter and checks for ascii characters lower than 32, replacing them with empty string -> "". I'm new to javascript so I was wondering whether anyone can point me towards the right direction ?

提前感谢您的时间。

推荐答案

试试这个:

var replaced = string.replaceAll("[^ -~]", "");

使用 ^ 否定字符类,因为空格是ASCII表中的字符32而且〜是最后一个可打印的字符,所以你基本上是说所有不是可打印的字符。

Using ^ negates the characters class, and since space is character 32 in the ASCII table and ~ is the last printable character, you're basically saying "everything that isn't a printable character".

简单来说删除0-31中的所有字符使用:

To simply remove all characters from 0-31 use:

var replace = string.replaceAll("\x00-\x1F", "");

这篇关于简单的ascii替换 - Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆