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

which is working fine for Java but it is not working for JavaScript might be backward slash have a some problem, please tell me how I can convert above java regex into Java Script.

1
  • 3
    FYI ~ validating emails with regular expressions is doomed to failure. If you must validate an email address, send an email to it with a confirmation code or something. Otherwise, you might as well simply use /.+@.+/ Commented Apr 3, 2014 at 5:50

3 Answers 3

10

Just reduce the double backslashes to singles. Also, you don't need to escape hyphen if it's the last character in a character class. Also also, you don't need to escape wildcard characters in a character class

Something like this

/([a-zA-Z0-9_-])([a-zA-Z0-9_.+~!#/$%^&*_='?-]*)@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z0-9]{2,})$/
Sign up to request clarification or add additional context in comments.

Comments

2

It depends on how you are using this? In what code?

you probably just need one \ everywhere you have two \\.

var re = new RegExp("ab+c");

or

var re = /ab+c/;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

2 Comments

It does need to escape the literal periods
I just realized what he was doing - yeah those were meant to be literals. I thought they were being escaped to get through the java interpolation only, but they were meant to be literals in the regex too. edited my response.
1

Regex Demo

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

Description

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.