我需要进行验证。我的文本框中不应允许使用以下模式(post Man/PostMan/post Man 等)。
[P|p](OST|ost).*\s*[M|m][a|A][N|n]\s*(\d.)*
我必须向同一输入添加更多验证,例如: 允许:
- 字母、数字和特殊字符“-”
并且,决不允许:
- 任何其他特殊字符
- 上面的书面模式,在我在文本框中输入的任何地方。
所以输入
- 我在等 postman
- 我正在等待 postman ###
- postman 有 不来,无效
然而,
- 我在等123348-12
- 我希望我的问题现在可以理解,有效
我尝试过以下方法:
([a-z 0-9 A-Z \\-])\*((?!\b[P|p]\*(OST|ost)\*\\.*\s\*[M|m][a|A][N|n]\s\*(\d.)\*\b))\*([a-z 0-9 A-Z])
但是一旦发现不匹配的字符,它就会停止工作。例如:
asdasd 666 # posttt -> 它停止验证特殊字符之后的任何内容。
我的正则表达式模式应该是什么?
最佳答案
描述
^(?!.*?[Pp]*(OST|ost)*\.*\s*[Mm][Aa][Nn]\s*(\d.)*)[a-z0-9A-Z-]*$
** 要更好地查看图像,只需右键单击图像并选择在新窗口中查看
此正则表达式将执行以下操作:
- 不允许第一个表达式匹配字符串中的任何位置
- 将仅允许
a-z
、A-Z
、0-9
和-
我想指出,如果您的字符串仅允许字母、数字和连字符,则无需在表达式中测试点。还可以使用不区分大小写标志来删除多个大写和小写字符集。
示例
现场演示
https://regex101.com/r/oY0hK2/1
示例文本
aWoeed1#fde39393aii
aWoeed1fde39393AII
aWoeed1fde39393AIIpostman 3a
示例匹配
aWoeed1fde39393AII
说明
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
[Pp]* any character of: 'P', 'p' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
OST 'OST'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
ost 'ost'
--------------------------------------------------------------------------------
)* end of \1 (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in \1)
--------------------------------------------------------------------------------
\.* '.' (0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[Mm] any character of: 'M', 'm'
--------------------------------------------------------------------------------
[Aa] any character of: 'A', 'a'
--------------------------------------------------------------------------------
[Nn] any character of: 'N', 'n'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of \2 (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in \2)
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-z0-9A-Z-]* any character of: 'a' to 'z', '0' to '9',
'A' to 'Z', '-' (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
关于JavaScript 正则表达式以避免文本框中的某些模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38355543/