JavaScript中所有可打印字符的正则表达式 [英] Regular expression for all printable characters in JavaScript

查看:943
本文介绍了JavaScript中所有可打印字符的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查找正则表达式,验证所有可打印字符。正则表达式只需要在JavaScript中使用。我已经浏览了这篇帖子,但它主要讨论的是.net,Java和C但是不是JavaScript。

Looking for a regular expression for that validates all printable characters. The regex needs to be used in JavaScript only. I have gone through this post but it mostly talks about .net, Java and C but not JavaScript.

您必须只允许这些可打印的字符:

You have to allow only these printable characters :

az,AZ,0-9,和三十二个符号:!#$%&'()* +, - 。/:;< =>?@ [] ^ _` {|}〜和空格

a-z, A-Z, 0-9, and the thirty-two symbols: !"#$%&'()*+,-./:;<=>?@[] ^_`{|}~ and space

需要一个JavaScript正则表达式验证输入字符是上述之一并丢弃其余字符。

Need a JavaScript regex to validate the input characters is one of the above and discard the rest.

推荐答案

如果你想匹配UTF-8集中的所有可打印字符(如8月21日的评论所示),那么你自己很难做到这一点.JavaScript的本地正则表达式支持非常糟糕的Unicode。但你可以使用 XRegExp 与正则表达式 ^ \P {C} * $

If you want to match all printable characters in the UTF-8 set (as indicated by your comment on Aug 21), you're going to have a hard time doing this yourself. JavaScript's native regexes have abysmal Unicode support. But you can use XRegExp with the regex ^\P{C}*$.

如果您只想将编辑中提到的那几个ASCII字母与8月22日的帖子相匹配,正则表达式是微不足道的:

If you only want to match those few ASCII letters you mentioned in the edit to your post from Aug 22, then the regex is trivial:

/^[a-z0-9!"#$%&'()*+,.\/:;<=>?@\[\] ^_`{|}~-]*$/i

这篇关于JavaScript中所有可打印字符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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