Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - With Space and Special Characters

I'm using the following Regex ^[a-zA-Z0-9]\s{2,20}$ for input

  • Letters A - Z
  • Letters a - z
  • Numbers 0 - 9

The input length must be a least 2 characters and maximum 20 characters.

I also want to enable space in the input, but only space, not new line, etc.

Last thing I have problem with is that I want to enable characters such as !@#$%^&*)(


2 Answers

add characters to your regex code like this~

^[a-zA-Z0-9 !@#$%^&*)(]{2,20}$

the \s is not only express space..

like image 142
Monday Avatar answered Oct 20 '25 02:10

Monday


Try ^[a-zA-Z0-9 ]{2,20}$.

And are you sure your original expression worked? The quantifier {2,20} is only applied to the \s, and not to your set inside [].

like image 23
Shi Avatar answered Oct 20 '25 00:10

Shi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!