Regex - With Space and Special Characters

2019-01-29 03:28发布

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 !@#$%^&*)(

4条回答
Summer. ? 凉城
2楼-- · 2019-01-29 03:46

All of Special Characters and char and number and with Space

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

查看更多
Evening l夕情丶
3楼-- · 2019-01-29 03:50

Regarding the second part of your question, just put those characters inside of [], no escaping needed.

查看更多
做个烂人
4楼-- · 2019-01-29 03:52

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 [].

查看更多
对你真心纯属浪费
5楼-- · 2019-01-29 03:53

add characters to your regex code like this~

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

the \s is not only express space..

查看更多
登录 后发表回答