Facebook注册表单电子邮件验证模式
Facebook registration form email validation pattern
当搜索RegExp模式来验证Javascript中的电子邮件地址时,我发现了Facebook从这里使用的模式。
function is_email(a){return /^(['w!.%+'-])+@(['w'-])+(?:'.['w'-]+)+$/.test(a);}
谁能给我解释一下这个模式是如何工作的?我知道它正在寻找三个位置的"单词字符"以及"@"字符。但是一个好的解释会对我理解这个有很大帮助。
有两个网站(据我所知),为正则表达式模式生成解释。
- regex101.com用文字解释模式
- regexper.com以图形方式显示
下面是我自己对这个模式的解释:
^ # anchor the pattern to the beginning of the string; this ensures that
# there are no undesired characters before the email address, as regex
# matches might well be substrings otherwise
( # starts a group (which is unnecessary and incurs overhead)
['w!.%+'-]
# matches a letter, digit, underscore or one of the explicitly mentioned
# characters (note that the backslash is used to escape the hyphen
# although that is not required if the hyphen is the last character)
)+ # end group; repeat one or more times
@ # match a literal @
( # starts another group (again unnecessary and incurs overhead)
['w'-] # match a letter, digit, underscore or hyphen
)+ # end group; repeat one or more times
(?: # starts a non-capturing group (this one is necessary and, because
# capturing is suppressed, this one does not incur any overhead)
'. # match a literal period
['w'-] # match a letter, digit, underscore or hyphen
+ # one or more of those
)+ # end group; repeat one or more times
$ # anchor the pattern to the end of the string; analogously to ^
所以,这将是一个稍微优化的版本:
/^['w!.%+'-]+@['w'-]+(?:'.['w'-]+)+$/
相关文章:
- HTML5模式未验证
- Rad网格编辑模式通过选中复选框时的java脚本启用禁用所需的字段验证器
- 使用angularJs ng模式进行10位Regex验证
- 我应该使用什么正则表达式来验证数字模式
- 在 PHP 中创建模式警报而不是页面重定向验证
- 如何验证多个模式
- 动态添加ng模式,缺少验证类
- 验证模式中的HTML5不适用于以一些特定字符开头的字符串
- ng模式验证在safari中不起作用
- 在量角器测试中验证引导模式上的项目
- 针对Grails后端的PhoneGap身份验证模式
- 如何正确验证模式表单
- 欧芹验证模式不接受空字符串
- 用于验证模式的正则表达式
- 如何在没有循环依赖的模型之间正确共享Hapi-Joi验证模式
- 如何在pouchDB中删除身份验证模式?
- jqGrid客户端验证模式在编辑最后一行时没有正确放置
- Facebook注册表单电子邮件验证模式
- 日期的验证模式无效
- JQuery验证模式问题