为你的下一个项目创建 3 个密码正则表达式

2025-06-10

为你的下一个项目创建 3 个密码正则表达式

正则表达式晦涩难懂,很难从头开始构建。
不过,不要被这些晦涩难懂的符号吓到,开发者社区可以提供帮助。
我在这里编写了三个密码正则表达式,供您在下一个 JavaScript 前端应用或 Node.js 后端应用中使用。

// PASSWORD REGEX FOR YOUR NEXT JAVASCRIPT APP

// regex for a basic password must be
// more than 8 chars 
const PASSWORD_REGEX_1=  /^[A-Za-z0-9]\w{8,}$/;

// more secure regex password must be
// more than 8 chars 
// at least one number
const PASSWORD_REGEX_2 = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/;

// more secure regex password must be :
// more than 8 chars  
// at least one number
// at least one special character
const PASSWORD_REGEX_3=  /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*]).{8,}$/;

Enter fullscreen mode Exit fullscreen mode

您可以组合所有 3 个正则表达式来显示适当的警告消息。

检查正则表达式的优秀网站:

  1. regex101
  2. 正则表达式
  3. 正则表达式测试器
鏂囩珷鏉ユ簮锛�https://dev.to/petroskoulianos/3-password-regex-for-your-next-project-53fn
PREV
TypeScript 实用程序:keyof 嵌套对象
NEXT
在您的 iOS 项目中使用 Bazel