How to detect underscore in regex?
本问题已经有最佳答案,请猛点这里访问。
为了测试我的密码,我有一个
1 | /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s])/ |
但下划线不被认为是
我该如何改进?
这里的问题是,对于javascript,
1 | [a-zA-Z0-9_] |
所以你是在含蓄地避开
您的regex的这一部分:
1 | (?=.*?[^\w\s]) // Match special character |
相当于:
1 | (?=.*?[^a-zA-Z0-9_\s]) |
参考文献
The \w metacharacter is used to find a word character.
A word character is a character from a-z, A-Z, 0-9, including the _
(underscore) character.
https://www.w3schools.com/jsref/jsref_regexp_wordchar.asp