Regular expression for all printable characters in JavaScript

前端 未结 4 1737
时光说笑
时光说笑 2020-11-27 20:33

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 talk

4条回答
  •  悲哀的现实
    2020-11-27 21:06

    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}*$.

    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
    

提交回复
热议问题