3

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

2
  • Remove (?<![_.]). Add (?!.*[_.]$) after ^. Or, /^(?!.*[_.]$)(?![_.0-9])(?!.*[_.]{2})[a-zA-Z0-9._]{0,20}$/ Commented May 29, 2020 at 14:11
  • Did it finally work, or do you need more help? Commented Dec 17, 2021 at 7:55

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

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.