Doby
Doby

Reputation: 91

Regex: Invalid group specifier name - react native

My regex works in browser but shows an error in react-native expo app (android) development

Regex:

/^(?=.{0,20}$)(?![_.0-9])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/

error:

Invalid regular expression: invalid group specifier name
no stack

How to fix this error, thanks

Upvotes: 3

Views: 2575

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626896

I suggest converting the (?<![_.]) lookbehind into a (?!.*[_.]$) lookahead and tighten it a bit (since the length can be checked with the consuming pattern part):

/^(?!.*[_.]$)(?![_.0-9])(?!.*[_.]{2})[a-zA-Z0-9._]{0,20}$/

Details

  • ^ - start of string
  • (?!.*[_.]$) - no . or _ allowed at the end
  • (?![_.0-9]) - no _, . and digit allowed at the start
  • (?!.*[_.]{2}) - no consecutive . or _ allowed anywhere
  • [a-zA-Z0-9._]{0,20} - 0 to 20 letters, digits, . or _
  • $ - end of string.

Upvotes: 1

Related Questions