簡體   English   中英

字符串的正則表達式必須包含一個大寫字母、一個小寫字母、一個數字、一個沒有空格的可打印 ASCI 字符

[英]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 正則表達式,它可以包含:

  • 一個大寫字母。 (至少 1 個)
  • 一個小寫字母。 (至少 1 個)
  • 一個數字(至少 1 個)
  • 可打印的 ASCII 字符(可選)
  • 沒有空格。

例子:

“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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM