Regular expression for all printable characters in JavaScript

Look for a regular expression to check for all printable characters. The regular expression should only be used in JavaScript. I went through this post, but it mainly talks about .net, Java and C, but not JavaScript.

You should only allow these printable characters:

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

The need for JavaScript regular expression to validate input characters is one of the above and discards the rest.

+7
source share
4 answers

If you want to match all the printable characters in the UTF-8 set (as indicated by your comment on August 21), it will be difficult for you to do this yourself. The original JavaScript regular expressions have terrible Unicode support. But you can use XRegExp with the regex ^\P{C}*$ .

If you only want to match those few ASCII letters that you indicated in editing your post since August 22, the regular expression is trivial:

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

For non-unicode, use the regex pattern ^[^\x00-\x1F\x80-\x9F]+$


If you want to work with unicode, first read Javascript + Unicode regexes .

I would then suggest using the regex pattern ^[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}]*$

  • \p{Cc} or \p{Control} : ASCII control character 0x00..0x1F or Latin-1 0x80..0x9F.
  • \p{Cf} or \p{Format} : an indicator of invisible formatting.
  • \p{Zl} or \p{Line_Separator} : U + 2028 line separator character.
  • \p{Zp} or \p{Paragraph_Separator} : U + 2029 paragraph separator character.

For more information see http://www.regular-expressions.info/unicode.html

+9
source

It looks like JavaScript has changed to some extent since this question was posted?

I use this one:

 var regex = /^[\u0020-\u007e\u00a0-\u00ff]*$/; console.log( regex.test("!\"#$%&'()*+,-./:;<=> ?@ [] ^_`{|}~")); //should output "true" console.log( regex.test("Iñtërnâtiônàlizætiøn")); //should output "true" console.log( regex.test("☃💩")); //should output "false" 
+6
source

To verify that a string consists of only printable ASCII characters, use a simple regular expression, for example

 /^[ -~]+$/ 

It matches

  • ^ - start of string binding
  • [ -~]+ - one or more (due to + quantifier characters) that are in the range from place to tilde in the ASCII table:

enter image description here
- $ - end of line binding

For Unicode characters for printing, use the \PC Unicode category (matching any char, but a char control)) from XRegExp , as already mentioned:

 ^\PC+$ 

See regex demos:

 // ASCII only var ascii_print_rx = /^[ -~]+$/; console.log(ascii_print_rx.test("It all right.")); // true console.log(ascii_print_rx.test('\f ')); // false, \f is an ASCII form feed char console.log(ascii_print_rx.test("demásiado tarde")); // false, no Unicode printable char support // Unicode support console.log(XRegExp.test('demásiado tarde', XRegExp("^\\PC+$"))); // true console.log(XRegExp.test('‌ ', XRegExp("^\\PC+$"))); // false, \u200C is a Unicode zero-width joiner console.log(XRegExp.test('\f ', XRegExp("^\\PC+$"))); // false, \f is an ASCII form feed char 
 <script src="http://cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.min.js"></script> 
+3
source

All Articles