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 ?

user3055126
  • 249
  • 3
  • 12
  • Does this answer your question? [no stack', reason: 'Unhandled JS Exception: Invalid regular expression: invalid group specifier name](https://stackoverflow.com/questions/56990784/no-stack-reason-unhandled-js-exception-invalid-regular-expression-invalid) – Muhammad Usama Ashraf Sep 13 '21 at 09:39

2 Answers2

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • The issue with this regex (which the original regex accounts for), is the following cases are not matched : somethiing@g.co something@co.uk something@c.co.uk somethign@com.co.uk – user3055126 Sep 13 '21 at 09:54
  • 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. – Tim Biegeleisen Sep 13 '21 at 09:58
  • 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 Sep 13 '21 at 10:00
  • 1
    @user3055126 You were right, I had a problem. Please try the update above. – Tim Biegeleisen Sep 13 '21 at 10:04
0

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

this works for me for simple email format abc@mail.com

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,}))$/;
vivaan
  • 26
  • 7