javascript - Facebook 注册表单电子邮件验证模式

标签 javascript regex facebook email-validation

当在 Javascript 中搜索 RegExp 模式来验证电子邮件地址时,我发现了 Facebook 使用的模式 here .

function is_email(a){return /^([\w!.%+\-])+@([\w\-])+(?:\.[\w\-]+)+$/.test(a);}

有人可以向我解释一下这种模式是如何工作的吗?据我所知,它正在三个位置寻找“单词字符”以及“@”字符。但一个好的解释会对我理解这一点有很大帮助。

最佳答案

有两个网站(据我所知)可以生成正则表达式模式的解释。

这是我自己对该模式的解释:

^        # 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\-]+)+$/

关于javascript - Facebook 注册表单电子邮件验证模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16318691/

相关文章:

javascript - Moment.js 处理德语 Dezember 和 Januar 的方式不同

javascript - DataTables API 方法不可用

iOS 8.3 及更高版本,未插入 Facebook 分享文本

android - facebook unity sdk获取用户的名字和姓氏:

python - 匹配字符串并提取部分

javascript - 使用 jquery 获取 facebook 点赞数出错

javascript - 在javascript中选择并添加类

javascript - jQuery 在单击按钮时将 php 文件中的值加载到 html 输入中 - 不起作用

javascript - 除非已经有链接,否则将 URL 转换为链接

c# - 正则表达式匹配逗号分隔的字符串,行尾没有逗号