正则表达式以任意顺序匹配至少两个特殊字符 [英] Regular expression to match at least two special characters in any order
问题描述
我必须对密码进行jQuery表单验证.
I have to do jQuery form validation for password.
密码应至少包含两个任意顺序的特殊字符.我尝试过 用于密码验证的正则表达式,但是它不能解决两个随机的特殊字符会出现的问题任何订单.
The password should contain at least two special characters in any order. I have tried with Regular Expression for password validation but it does not address that two random special characters can come at any order.
如何使用JavaScript正则表达式来做到这一点?
How do I do it using a JavaScript regular expression?
推荐答案
在不必要的情况下,您不必使用环视.
You do not have to use look-arounds in cases when you do not have to.
如果只需要确保字符串中至少包含2个特定集合的字符,请使用这种正则表达式(使用否定的类使其更健壮):
If you only need to make sure the string has at least 2 characters of a specific set, use this kind of a regex (with a negated class to make it more robust):
/(?:[^`!@#$%^&*\-_=+'\/.,]*[`!@#$%^&*\-_=+'\/.,]){2}/
请参见演示
这篇关于正则表达式以任意顺序匹配至少两个特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!