Regex for password policy with trim function

78 Views Asked by At

I need to create a regex for a password policy.

  • The password has to contain at least 6 characters.
  • The password has to contain at least one lowercase letter.
  • The password has to contain at least one upercase letter.
  • The password has to contain at least one special character.
  • Whitespaces in the middle of the password are allowed, but not at the beginning and at the end (=> trim function).

My interim result is this expression:

^(?=.* [A-Z])(?=.* [a-z])(?=.*\W).{6,}$

It works for the first four criterions, but not for the trim function.

I tried something linke this:

(?=\S[\s]* )(?=.* [A-Z])(?=.* [a-z])(?=.* \W).{6,}$

but this doesn't work, because whitespaces at the end are still possible.

Could anyone help me to extend my result, so that the regex allows whitespaces in the middle of the password, but not at the beginning and not at the end?

0

There are 0 best solutions below