具有字母、数字和一些特殊字符的文本框验证的正则表达式

时间:2022-08-28 22:29:02

I am trying to validate a text box using regular expression. I want to add alphabets, numbers and some special characters like "& - / . # , space". I have tried with this

/^[a-zA-Z0-9&-/.#,\s]*$/.

It is working fine except the *. I want to prevent * symbol.

我正在尝试使用正则表达式验证一个文本框。我想添加字母、数字和一些特殊字符,如“& - /”。#空间”。我试过/ ^[a-zA-Z0-9& - /。#,\]* /美元。除*外,一切正常。我想防止*符号。

2 个解决方案

#1


1  

You should escape the hyphen and slash:

你应该避开连字符和斜线:

/^[a-zA-Z0-9&\-\/.#,\s]*$/

&-/ is interpreted as a range, just like a-z, which include * and some other characters.
Escaping the slash is not mandatory here, but I always try to escape it in regex literals (/this syntax/).

&-/被解释为一个范围,就像a-z,包括*和其他一些字符。在这里,转义斜杠不是必须的,但是我总是尝试在regex常量中转义斜杠(/this syntax/)。

#2


1  

Put & at the end of regex Like below

把&放在regex的末尾,如下所示

/^[a-zA-Z0-9-/.#,\s&]*$/

/ / ^[a-zA-Z0-9。#,\ &]* /美元

#1


1  

You should escape the hyphen and slash:

你应该避开连字符和斜线:

/^[a-zA-Z0-9&\-\/.#,\s]*$/

&-/ is interpreted as a range, just like a-z, which include * and some other characters.
Escaping the slash is not mandatory here, but I always try to escape it in regex literals (/this syntax/).

&-/被解释为一个范围,就像a-z,包括*和其他一些字符。在这里,转义斜杠不是必须的,但是我总是尝试在regex常量中转义斜杠(/this syntax/)。

#2


1  

Put & at the end of regex Like below

把&放在regex的末尾,如下所示

/^[a-zA-Z0-9-/.#,\s&]*$/

/ / ^[a-zA-Z0-9。#,\ &]* /美元