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