我试图在javascript中创建regex并进行测试。
规则:文本应该只有8个字符。只包含大写和数字1-5,没有小写或特殊字符或6-7个数字.
示例:
12345678 -- false
a2345678 -- false
aabbccdd -- false
AABB33DD -- true // contains BOTH uppercase and
numbers between 1-5 and nothing else
AABB88DD -- false
AABBCCDD -- false
AABB3.DD -- false
代码:
var pattern = new RegExp("^(?=.*[1-5])(?=.*[A-Z]).+$");
pattern.test(code)
我找不到合适的食客了。有人能帮忙吗?
发布于 2022-03-15 21:32:20
使用
^(?=.*[1-5])(?=.*[A-Z])[A-Z1-5]{8}$
见正则证明。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[1-5] any character of: '1' to '5'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[A-Z1-5]{8} any character of: 'A' to 'Z', '1' to '5'
(8 times)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
https://stackoverflow.com/questions/71489198
复制相似问题