DEV Community

bikashgosai
bikashgosai

Posted on

Regex that starts with letter, contains 1 uppercase/lowercase letter, one number and no special characters & min 8 characters

You need to put the lookaheads after ^ and put [a-zA-Z] right after them and quantify the rest with {8,}:

^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])[a-zA-Z][a-zA-Z0-9]{8,}$

Regex that starts with letter, contains 1 uppercase/lowercase letter, one number and special characters & min 8 characters

^(?=.*[0-9])(?=.*[!@#$%^&*])[0-9a-zA-Z!@#$%^&*0-9]{8,}$
Enter fullscreen mode Exit fullscreen mode

See the regex demo.
https://regex101.com/r/fGk1AC/1

> Pattern details:

  1. ^ - start of a string
  2. (?=.*?[a-z]) - at least 1 lowercase ASCII letter
  3. (?=.*?[A-Z]) - at least 1 uppercase ASCII letter
  4. (?=.*?[0-9]) - at least 1 ASCII digit 5.[a-zA-Z] - an ASCII letter
  5. [a-zA-Z0-9]{7,} - 7 or more ASCII letters or digits (\w also allows _)
  6. $ - end of string.

Discussion (0)