Sometimes, we want to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase.
In this article, we’ll look at how to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase.
How to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase?
To create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase, we can create a regex with all the patterns in it.
For instance, we write
const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/;
to define the regex that matches all the patterns.
(?=.*\d) matches at least one digit.
(?=.*[a-z]) matches at least one lower case.
(?=.*[A-Z]) matches at least one upper case.
[a-zA-Z0-9]{8,} matches at least 8 from the mentioned characters.
Conclusion
To create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase, we can create a regex with all the patterns in it.