正则表达式 - 带空格和特殊字符
我使用以下正则表达式 ^[a-zA-Z0-9]\s{2,20}$
输入
- 字母 A - Z
- 字母 a - z
- 数字 0 - 9
输入长度必须至少 2 个字符,最多 20 个字符。
我还想在输入中启用空格,但只有空格,而不是新行等。
我遇到的最后一件事是我想启用诸如 !@#$%^&*)(< /代码>
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 !@#$%^&*)(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像这样将字符添加到您的正则表达式代码中〜
\s
不仅仅是表达空间..add characters to your regex code like this~
the
\s
is not only express space..尝试
^[a-zA-Z0-9 ]{2,20}$
。你确定你原来的表达有效吗?量词
{2,20}
仅应用于\s
,而不应用于[]
内的集合。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[]
.关于问题的第二部分,只需将这些字符放入
[]
内,无需转义。Regarding the second part of your question, just put those characters inside of
[]
, no escaping needed.所有特殊字符、字符和数字以及空格
[A-Za-z0-9-.& ,+!@#$%\^*();\/|<>"'?=:\t_\n[]{}~`]
All of Special Characters and char and number and with Space
[A-Za-z0-9-.& ,+!@#$%\^*();\/|<>"'?=:\t_\n[]{}~`]
*** 所有类型的特殊字符和普通字符的正则表达式,它们之间也有空格。
在某个变量中声明以下内容并执行您的任务:
*** 所有类型带空格的特殊字符的正则表达式:
(答案经过尝试和测试!!)
*** Regex for all types of special characters and normal characters too with space in between them.
Declare the below in some variable and perform your task:
*** Regex for all types of special characters with spaces:
(answer is tried and tested!!)