Apurv Jain
Apurv Jain

Reputation: 121

Convert Java Regex to javascript regex

([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.

Upvotes: 9

Views: 19785

Answers (3)

abc123
abc123

Reputation: 18763

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

Upvotes: 1

DrLivingston
DrLivingston

Reputation: 818

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

Upvotes: 2

Phil
Phil

Reputation: 164766

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,})$/

Upvotes: 10

Related Questions