RegEx to make sure that the string contains at least one lower case char, upper case char, digit and symbol

后端 未结 3 1111
悲哀的现实
悲哀的现实 2020-11-22 16:06

What is the regex to make sure that a given string contains at least one character from each of the following categories.

  • Lowercase character
  • Upperca
3条回答
  •  有刺的猬
    2020-11-22 16:37

    If you need one single regex, try:

    (?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)
    

    A short explanation:

    (?=.*[a-z])        // use positive look ahead to see if at least one lower case letter exists
    (?=.*[A-Z])        // use positive look ahead to see if at least one upper case letter exists
    (?=.*\d)           // use positive look ahead to see if at least one digit exists
    (?=.*\W])        // use positive look ahead to see if at least one non-word character exists
    

    And I agree with SilentGhost, \W might be a bit broad. I'd replace it with a character set like this: [-+_!@#$%^&*.,?] (feel free to add more of course!)

提交回复
热议问题