Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for all printable characters in 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 :

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

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

like image 545
AurA Avatar asked Aug 21 '12 10:08

AurA


1 Answers

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
like image 181
Tim Pietzcker Avatar answered Sep 17 '22 03:09

Tim Pietzcker