有人可以解释这个电子邮件正则表达式的含义

时间:2013-05-02 18:13:03

标签: javascript regex validation

^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))
    @((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)
  +[a-zA-Z]{2,}))$

我只能理解正则表达式的部分而不是整个表达式,比如

([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)

匹配一个或多个不是字符

的字符
<>()[\]\\.,;:\s@\"  \\  Not sure what this [\]\\  and \s@\ means

我也能理解其他一些部分,但不能理解为单个实体。

3 个答案:

答案 0 :(得分:5)

你走了:

^(
    (
        [^<>()[\]\\.,;:\s@\"]+ // Disallow these characters any amount of times
        (
            \. // Allow dots
            [^<>()[\]\\.,;:\s@\"]+ // Disallow these characters any amount of times
        )* // This group must occur once or more
    )
    | // or
    (\".+\") // Anything surrounded by quotes (Is this even legal?)
)
@ // At symbol is litterally that
(
    // IP address
    (
        \[ // Allows square bracket
        [0-9]{1,3} // 1 to three digits (for an IP address
        \. // Allows dot
        [0-9]{1,3} // 1 to three digits (for an IP address
        \. // Allows dot
        [0-9]{1,3} // 1 to three digits (for an IP address
        \. // Allows dot
        [0-9]{1,3} // 1 to three digits (for an IP address
        \] // Square bracket
    ) 
    | // OR a domain name
    (
        ([a-zA-Z\-0-9]+\.) // Valid domain characters are a-zA-Z0-9 plus dashes
        +
        [a-zA-Z]{2,} // The top level (anything after the dot) must be at least 2 chars long and only a-zA-Z
    )
)$

答案 1 :(得分:5)

这是一个来自debuggex.com的简单插图

First group

Second Group

答案 2 :(得分:4)

“不确定此[\]\\\s@\"的含义是什么”

\]是转发的]
\\是一个已转义的\ \s是任何空格 @@ \"是转发的"

“+是什么意思”

+表示+

之前的“一个或多个”