[英]Regex Expression allowing special characters
I'm using an online regex checker such as regex101 to check my regex which is to be used by javascript such as ( working but cut down regex for example only) 我正在使用在线正则表达式检查器(例如regex101)来检查我的正则表达式,该正则表达式将由javascript使用(例如( 工作但仅减少正则表达式))
state = /^[a-zA-Z0-9]$/.test($(control).val())
My Regex is 我的正则表达式是
(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9].{5,19}
Which when cut down into chunks should hopefully mean... 切成块时应该指的是...
(?=.*[A-Z]) - Must include an Upper case char
(?=.*[0-9]) - Must include a numeric
[a-zA-Z0-9. ] - Can include any lower or upper case char, or Numeric, a period or space
. - Matching previous
{5,19} - the string must be 6-20 characters in length
This however still allows special characters such as !. 但是,这仍然允许使用特殊字符,例如!。
I've not used \\d for decimals as I believe [0-9] should be more strict in this regard, and removed the period and space to see whether this was the cause, to no avail. 我没有将\\ d用作小数,因为我认为[0-9]在这方面应该更严格,因此删除了句点和空格以查看是否是原因,但无济于事。
Where have I gone wrong to be allowing special characters? 允许使用特殊字符在哪里出错了?
You need to remove the last .
您需要删除最后一个
.
which you think is matching previous, it actually matches any character except for newline, so this is where the ! 您认为与先前匹配的字符,它实际上与除换行符之外的任何字符都匹配,所以这里就是! is getting through.
正在通过。
So (?=.*[AZ])(?=.*[0-9])[a-zA-Z0-9].{5,19}
should be (?=.*[AZ])(?=.*[0-9])[a-zA-Z0-9]{5,19}
因此
(?=.*[AZ])(?=.*[0-9])[a-zA-Z0-9].{5,19}
应该是(?=.*[AZ])(?=.*[0-9])[a-zA-Z0-9]{5,19}
Also just thought i'd mention there is no difference between \\d
and [0-9]
whatsoever. 也只是以为我会提到
\\d
和[0-9]
之间没有任何区别。
UPDATED - this following should fix the issues you were seeing with the regex - (?=.*[AZ])(?=.*[0-9])[a-zA-Z0-9\\.\\s]{6,20}$
更新-以下内容应解决正则表达式中遇到的问题-
(?=.*[AZ])(?=.*[0-9])[a-zA-Z0-9\\.\\s]{6,20}$
\\.
\\.
(to allow a .) \\s
(to allow whitespace characters) \\s
(允许空格字符) {5, 19}
to {6, 20}$
to ensure the correct character match {5, 19}
更改为{6, 20}$
以确保正确的字符匹配 If you want to test this version of the regex in regex101 here 如果你想在regex101测试这个版本的正则表达式的位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.