![](/img/trans.png)
[英]Regex for password: Must start with a capital letter and contain only one digit
[英]Regex for a string that must contain an uppercase letter, a lower case letter, a digit, a printable ASCI character with no white spaces
幫我為一個字符串編寫一個 JavaScript 正則表達式,它可以包含:
例子:
“NewYork12@”可以是有效字符串。 “New York12@”是無效字符串。
我試過的:
/(?=.*[az])(?=.*[AZ])(?=.*\\d)[A-Za-z\\d](?=.*[!-~])/
這不起作用,因為它也接受空格。
謝謝。
我只想使用\\S+
作為要在這里匹配的實際模式,保持你的積極前瞻:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)\S+$/
這個模式說:
^ from the start of the input
(?=.*[a-z]) assert lowercase letter present
(?=.*[A-Z]) assert uppercase letter present
(?=.*\d) assert digit present
\S+ then match one or more exclusively non whitespace characters
$ end of the input
否定匹配可能很難在正則表達式中正確表達。 不將所有內容都表達為單個正則表達式通常更簡單。 使用多個正則表達式並將它們的結果組合到宿主語言中:
if (
input.match(/[A-Z]/) // at least 1 upper case letter
&& input.match(/[a-z]/) // at least 1 lower case letter
&& input.match(/[0-9]/) // at least 1 digit
&& !input.match(/\s/) // no whitespace
) {
// all rules fulfilled
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.