0

I am using the following regex in my react-native app.

This is an email validation regex :

^[\w]+@((?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+)(?:[A-Za-z0-9-]{2,63}(?<!-))

This works fine in the browser but crashes the react native app due to the following :

no stack', reason: 'Unhandled JS Exception: Invalid regular expression: invalid group specifier name

Could someone please help getting this to work on react native, maybe by achieving the same thing that this regex achieves but without the lookbehind expression ?

1

2 Answers 2

1

The problem is likely caused the by the (?<!-) negative lookahead at the end of the regex pattern, which your JavaScript engine does not support. To ensure that a hyphen does not occur at the end of the email, we can simply use:

^[\w]+@((?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+)(?:[A-Za-z0-9-]{1,62}[A-Za-z0-9])

That is, just use [A-Za-z0-9] to represent the final of 63 possible characters in the pattern.

Sign up to request clarification or add additional context in comments.

4 Comments

The issue with this regex (which the original regex accounts for), is the following cases are not matched : [email protected] [email protected] [email protected] [email protected]
My answer should be logically equivalent to what you have above, and it should fully address the incompatibility issue. If you have some other issue with your current regex, you might want to open a new question.
Can you please explain how it is logically equivalent if the original regex matched certain emails that the new regex you provided isn't ?
@user3055126 You were right, I had a problem. Please try the update above.
0

This regex will not work likely problem in regex pattern i think.

this works for me for simple email format [email protected]

var regex = /^(([^<>()\[\]\\.,;:\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,}))$/;

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.