Password regex for specific requirements

前端 未结 1 1017
耶瑟儿~
耶瑟儿~ 2021-01-23 05:35

I am to write a regex for following requirements

  1. At least one character
  2. At least one digit
  3. Length must be of 8
  4. At least one special char
1条回答
  •  醉话见心
    2021-01-23 05:49

    You can solve these with a combination of lookaheads:

    1. (?=.*[a-zA-Z])
    2. (?=.*\d)
    3. .{8}
    4. (?=.*[^\da-zA-Z])

    The last one just requires a non-letter and non-digit which is probably by far the easiest way of specifying that you want a somewhat “special” character.

    So in the end you have

    ^(?=.*[a-zA-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8}$
    

    0 讨论(0)
提交回复
热议问题