0

What is the regex to make sure that a given string contains at least one lowercase character and one uppercase character but also my include number s and special characters !@#$%^&*()+=? ?

Does the order of the regex matter?

  • It would help to know which regex engine or text editor you are using, but generally speaking, a [lookahead](https://www.regular-expressions.info/lookaround.html) would give you this flexibility. – Kenneth K. Oct 16 '18 at 17:21
  • I use this for password regex, at least 1 uppercase, 1 lowercase, 1 number, 1 special character, minimum 10 characters: `/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{10,})/` from https://www.thepolyglotdeveloper.com/2015/05/use-regex-to-test-password-strength-in-javascript – user1063287 Oct 16 '18 at 17:26
  • Possible duplicate of [RegEx to make sure that the string contains at least one lower case char, upper case char, digit and symbol](https://stackoverflow.com/questions/1559751/regex-to-make-sure-that-the-string-contains-at-least-one-lower-case-char-upper) – Poul Bak Oct 16 '18 at 17:55

1 Answers1

1

You can use positive lookahead patterns to ensure that there are at least one uppercase and one lowercase characters, while using a character set to cover the rest of the allowed characters:

^(?=[a-z0-9!@#$%^&*()+=?]*[A-Z])(?=[A-Z0-9!@#$%^&*()+=?]*[a-z])[A-Za-z0-9!@#$%^&*()+=?]*$

Demo: https://regex101.com/r/Uyy1aj/2

blhsing
  • 91,368
  • 6
  • 71
  • 106