[英]Update on Devise regex for Email validation
I'm currently using Devise regex :我目前正在使用设计正则表达式:
/\\A[^@\\s]+@[^@\\s]+\\z/
Which is simple and sort.这是简单和排序。 But, it doesn't filter email like example@example..com
.但是,它不会像example@example..com
那样过滤电子邮件。
An alternative to that is URI::MailTo::EMAIL_REGEXP :另一种方法是URI::MailTo::EMAIL_REGEXP :
/\\A[a-zA-Z0-9.!\\#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\\z/
Which will filter valid domains like hans@über.com
.这将过滤有效域,如hans@über.com
。
I'm not good at regex, but is there something better or how an I add a double dot validation in the first one with Devise.我不擅长正则表达式,但是有没有更好的东西或者我如何在第一个使用 Devise 添加双点验证。
You may refactor your regex as您可以将您的正则表达式重构为
/\A[^@\s]+@[^@.\s]+(?:\.[^@.\s]+)*\z/ # If you want to allow name@domain
/\A[^@\s]+@[^@.\s]+(?:\.[^@.\s]+)+\z/ # If you want to require at least one dot after @
Here,这里,
\\A
- start of string \\A
- 字符串的开始[^@\\s]+
- a negated character class that matches 1 or more characters other than @
and whitespace [^@\\s]+
- 匹配 1 个或多个字符( @
和空格以外的字符)的否定字符类@
- a @
char @
- 一个@
字符[^@.\\s]+
- 1 or more characters other than @
, .
[^@.\\s]+
- 除@
、 .
之外的 1 个或多个字符.
and whitespace和空格(?:\\.[^@.\\s]+)*
- 0 or more occurrences (or 1 or more if +
is used at the end) of (?:\\.[^@.\\s]+)*
- 0 次或多次出现(或 1 次或更多,如果在末尾使用+
)
\\.
- a dot - 一个点[^@.\\s]+
- 1 or more characters other than @
, .
[^@.\\s]+
- 除@
、 .
之外的 1 个或多个字符.
and whitespace和空格\\z
- end of string. \\z
- 字符串的结尾。See regex #1 demo and regex #2 demo .请参阅正则表达式 #1 演示和正则表达式 #2 演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.