javascript - 为什么我的验证电子邮件地址的正则表达式会出现“无需重复”?
问题描述
我正在学习如何使用正则表达式来验证具有以下标准的电子邮件地址:
如果其域和扩展名是example123.com并且user的值满足以下约束,则user@domain.extension有效:
- 它以字符类 [az] 表示的 1 到 6 个小写英文字母开头
- 小写字母后跟可选的下划线,即 _ 字符出现 0 次或 1 次
- 可选下划线后跟 0 到 4 个可选数字,由字符类 [0-9] 表示
理想情况下...
re.test(logic@example123.com) --> should return true
re.test(logic_@example123.com) --> should return true
re.test(logic_0@example123.com) --> should return true
re.test(logic2_0@example123.com) --> should return false
re.test(logic_0@gmail.com) --> should return false
到目前为止,我有:const re = /^[a-z]{1,6}[_?][\d?]{0,4}+@[example123\.com]$/ig
我收到一个错误“无需重复”。我已经浏览了其他 stackoverflow 帖子,但仍然无法完全理解为什么我的模式不起作用。
解决方案
在\d{0,4}+
中,+
不能是 的量词{0,4}
,所以它有“没什么可重复的”。它可能是\d{0,4}
或\d+
。
此外,方括号用于字符范围或文字字符。不要在 中使用它们[_?]
,这将在此处解释为搜索_
或?
。
由于您不希望电子邮件中出现大写字母,因此您必须删除i
正则表达式中忽略大小写的修饰符。
尝试这个:
const re = /^[a-z]{1,6}_?\d{0,4}@example123\.com$/
console.log(re.test("logic@example123.com"));
推荐阅读
- java - 如何在 Retrofit 上上传没有标题和容器的文件
- android-studio - 更新插件后的警告
- sql - 选择具有特定值的列(虚拟表)
- c# - 从 HelixViewport3D 创建图像
- javascript - 使用 localStorage 保存多个内容可编辑输入
- vue.js - 所有父子关系 Vue 组件
- azure - 如何在 Azure Pipelines 拉取请求中重新部署版本?
- sql-server - SQL Server Express 的 CMD 安装
- ios - iOS 位置权限请求仅显示英文,而其他键正在工作
- python - matplotlib 中的正符号刻度标签