js中正则表达式实现验证首字符为大写英文字符,其后为大写英文或数字或中线,最后为汉字或乘号
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js中正则表达式实现验证首字符为大写英文字符,其后为大写英文或数字或中线,最后为汉字或乘号相关的知识,希望对你有一定的参考价值。
js中正则表达式实现验证首部分为大写英文字符,其后为大写英文或数字或中线,最后为汉字或Φ号,也就是实现下边的
例如:ASD-123首或ASD24-123或AS7D-123Φ
只能输入汉字:"^[\u4e00-\u9fa5]0,$"
用于密码验证的正则表达式 [重复]
【中文标题】用于密码验证的正则表达式 [重复]【英文标题】:Regex for Password Validation [duplicate] 【发布时间】:2016-10-09 10:37:55 【问题描述】:我正在寻找一个正则表达式来验证以下密码:
密码必须最少 7 个字符,最多 50 个字符密码必须包含以下四个类别中的至少三个:
-
英文大写字母字符 (A–Z)
英文小写字母字符 (a–z)
以 10 位为基数 (0–9)
非字母数字字符(例如,!$#,%)
我的尝试如下:
^.*(?=.7,50)(?=.*\d)(?=.*[A-Z]).*$
这不检查特殊字符,我不知道如何使密码必须包含以下四个类别中的至少三个。
谁能帮忙推荐一个正则表达式来验证这个密码策略?
【问题讨论】:
这是否有某种原因需要成为正则表达式?您的每个约束都可以使用内置函数来实现,或者最多可以使用几行琐碎的代码,这将比正则表达式解决方案更易于理解和维护。 【参考方案1】:说明
^(?:(?=.*?[A-Z])(?:(?=.*?[0-9])(?=.*?[-!@#$%^&*()_[\],.<>+=])|(?=.*?[a-z])(?:(?=.*?[0-9])|(?=.*?[-!@#$%^&*()_[\],.<>+=])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%^&*()_[\],.<>+=]))[A-Za-z0-9!@#$%^&*()_[\],.<>+=-]7,50$
为了更好地查看图像,您可以右键单击图像并选择在新窗口中查看。
这个正则表达式将执行以下操作
要求字符串长度为 7 - 50 个字符 允许字符串包含A-Z
、a-z
、0-9
和!@#$%^&*()_[\],.<>+=-
字符
需要至少一个字符来自以下任意三种情况
-
英文大写字母
A–Z
英文小写字母字符a–z
以 10 位为基数 0–9
非字母数字字符!@#$%^&*()_[],.<>+=-
示例
现场演示
https://regex101.com/r/jR9cC7/1
示例文本
1 2 3 4 5 6
12345678901234567890123456789012345678901234567890124567890
aaaaAAAA1111
aaaaBBBBBBB
AAAAaaaa__
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!AA
允许的字符串
aaaaAAAA1111
AAAAaaaa__
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A
说明
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[- any character of: '-', '!', '@',
!@#$%^&*()_[\] '#', '$', '%', '^', '&', '*', '(',
,.<>+=] ')', '_', '[', '\]', '', '', ',',
'.', '<', '>', '+', '='
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[- any character of: '-', '!', '@',
!@#$%^&*()_[ '#', '$', '%', '^', '&', '*', '(',
\],.<>+=] ')', '_', '[', '\]', '', '',
',', '.', '<', '>', '+', '='
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
[- any character of: '-', '!', '@', '#',
!@#$%^&*()_[\] '$', '%', '^', '&', '*', '(', ')',
,.<>+=] '_', '[', '\]', '', '', ',', '.',
'<', '>', '+', '='
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
[A-Za-z0- any character of: 'A' to 'Z', 'a' to 'z',
9!@#$%^&*()_[\],.< '0' to '9', '!', '@', '#', '$', '%', '^',
>+=-]7,50 '&', '*', '(', ')', '_', '[', '\]', '',
'', ',', '.', '<', '>', '+', '=', '-'
(between 7 and 50 times (matching the most
amount possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
----------------------------------------------------------------------
【讨论】:
非常感谢。效果很好【参考方案2】:为了在一个正则表达式中添加多个条件,我们使用管道或括号 /(regex1)|(regex2)|(regex2)/ 或 (regex1)(regex2)
每个正则表达式条件都非常基本,对于您提出的每个条件都有很多示例。
你可以看这里:
Regex for Password Must be contain at least 8 characters, least 1 number and both lower and uppercase letters and special characters
【讨论】:
以上是关于js中正则表达式实现验证首字符为大写英文字符,其后为大写英文或数字或中线,最后为汉字或乘号的主要内容,如果未能解决你的问题,请参考以下文章
用于验证大写字母全名的正则表达式(包括 unicode 字符)?