javascript 正则表达式匹配至少一个大写字母和至少一位数字和任意数量的特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23711508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:26:05  来源:igfitidea点击:

Regular expression to match at least One capital letter and at least One digit and any number of special charecter

javascriptregex

提问by PaRsH

Hi I'm new to this regular expression. Please help me on this query.

嗨,我是这个正则表达式的新手。请帮我解决这个问题。

I want the Regular expression to match at least One capital letter and at least One digit and any number of special characters. Minimum length 8 and maximum length can be 15.

我希望正则表达式至少匹配一个大写字母和至少一个数字以及任意数量的特殊字符。最小长度为 8,最大长度可以为 15。

Note : The special characters allowed are @#$&.

注意:允许的特殊字符是@#$​​&。

Thanks for your help.

谢谢你的帮助。

回答by PaRsH

Thanks guys. I found the answer.

多谢你们。我找到了答案。

/^(?=.*\d)(?=.*[A-Z])(?!.*[^a-zA-Z0-9@#$^+=])(.{8,15})$/

回答by Gaurang Tandon

Regex:

正则表达式:

[A-Z]+[0-9]+[@#$&]*

And for the length part, use:

对于长度部分,请使用:

var len = str.length;

if( /[A-Z]/.test(str) && /[0-9]/.test(str) && len >= 8 && len <= 15 )

[A-Z]- one capital letter
[0-9]- one digit

[A-Z]- 一个大写字母
[0-9]- 一位数字

[abc]means any of a, b, or c.

[abc]指任何的abc

http://regex101.com/r/aE5hL4

http://regex101.com/r/aE5hL4

回答by Leo

This should work (unless you want to match newlines too):

这应该有效(除非您也想匹配换行符):

/(?:[A-Z].*[0-9])|(?:[0-9].*[A-Z])/

(I missed the length restriction, but anyway you seem satisfied with what you got there.)

(我错过了长度限制,但无论如何你似乎对你得到的东西很满意。)