将Java正则表达式转换为JavaScript正则表达式。

8
([a-zA-Z0-9_\\-])([a-zA-Z0-9_\\.+~!#/$%^&*_=\\'?\\-]*)@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z0-9]{2,})$

该正则表达式在Java中工作正常,但在JavaScript中不起作用,可能是反斜杠存在问题,请告诉我如何将上述Java正则表达式转换为JavaScript。


3
请注意,使用正则表达式验证电子邮件地址注定会失败。如果你必须要验证一个电子邮件地址,最好发送一封带有确认代码的电子邮件到该地址。否则,你不妨直接使用 /.+@.+/。请让我知道如何协助您。 - Phil
3个回答

9

只需将双反斜杠减少为单个即可。如果连字符是字符类中的最后一个字符,则无需转义它。而且,您无需在字符类中转义通配符字符。

像这样

/([a-zA-Z0-9_-])([a-zA-Z0-9_.+~!#/$%^&*_='?-]*)@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z0-9]{2,})$/

1

1
它确实需要转义字面上的句点。 - Phil
我刚意识到他在做什么 - 是的,那些应该是字面量。我以为它们只是被转义以通过Java插值,但它们在正则表达式中也应该是字面量。我编辑了我的回复。 - DrLivingston

0

正则表达式演示

var myregexp = /([a-zA-Z0-9_\-])([a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]*)@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z0-9]{2,})$/;

Regular expression visualization

Debuggex Demo

描述

1st Capturing group ([a-zA-Z0-9_\-])
    [a-zA-Z0-9_\-] match a single character present in the list below
        a-z a single character in the range between a and z (case sensitive)
        A-Z a single character in the range between A and Z (case sensitive)
        0-9 a single character in the range between 0 and 9
        _ the literal character _
        \- matches the character - literally
2nd Capturing group ([a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]*)
    [a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]* match a single character present in the list below
        Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        a-z a single character in the range between a and z (case sensitive)
        A-Z a single character in the range between A and Z (case sensitive)
        0-9 a single character in the range between 0 and 9
        _ the literal character _
        \. matches the character . literally
        +~!# a single character in the list +~!# literally
        \/ matches the character / literally
        $%^&*_= a single character in the list $%^&*_= literally (case sensitive)
        \' matches the character ' literally
        ? the literal character ?
        \- matches the character - literally
    @ matches the character @ literally
[A-Za-z0-9-]+ match a single character present in the list below
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    A-Z a single character in the range between A and Z (case sensitive)
    a-z a single character in the range between a and z (case sensitive)
    0-9 a single character in the range between 0 and 9
    - the literal character -
3rd Capturing group (\.[A-Za-z0-9-]+)*
    Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
    \. matches the character . literally
[A-Za-z0-9-]+ match a single character present in the list below
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    A-Z a single character in the range between A and Z (case sensitive)
    a-z a single character in the range between a and z (case sensitive)
    0-9 a single character in the range between 0 and 9
    - the literal character -
4th Capturing group (\.[A-Za-z0-9]{2,})
    \. matches the character . literally
    [A-Za-z0-9]{2,} match a single character present in the list below
        Quantifier: Between 2 and unlimited times, as many times as possible, giving back as needed [greedy]
        A-Z a single character in the range between A and Z (case sensitive)
        a-z a single character in the range between a and z (case sensitive)
        0-9 a single character in the range between 0 and 9
    $ assert position at end of the string

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接