Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
([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.

share|improve this question
    
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 /.+@.+/ –  Phil Apr 3 '14 at 5:50

3 Answers 3

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,})$/
share|improve this answer

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

share|improve this answer
1  
It does need to escape the literal periods –  Phil Apr 3 '14 at 5:41
    
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. –  DrLivingston Apr 3 '14 at 5:42

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.