正则表达式 - 带空格和特殊字符

发布于 2024-11-27 07:55:12 字数 254 浏览 11 评论 0原文

我使用以下正则表达式 ^[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌逼全场 2024-12-04 07:55:12

像这样将字符添加到您的正则表达式代码中〜

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

\s 不仅仅是表达空间..

add characters to your regex code like this~

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

the \s is not only express space..

埖埖迣鎅 2024-12-04 07:55:12

尝试 ^[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 [].

风为裳 2024-12-04 07:55:12

关于问题的第二部分,只需将这些字符放入 [] 内,无需转义。

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

耳钉梦 2024-12-04 07:55:12

所有特殊字符、字符和数字以及空格

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

All of Special Characters and char and number and with Space

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

香草可樂 2024-12-04 07:55:12

*** 所有类型的特殊字符和普通字符的正则表达式,它们之间也有空格。

在某个变量中声明以下内容并执行您的任务:

/^[a-zA-Z0-9 !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{2,20}$/

*** 所有类型带空格的特殊字符的正则表达式:

/^[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{2,20}$/

(答案经过尝试和测试!!)

*** 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:

/^[a-zA-Z0-9 !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{2,20}$/

*** Regex for all types of special characters with spaces:

/^[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{2,20}$/

(answer is tried and tested!!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文