1

I've been trying to resolve this all day.

Here are the requirements:
- I want to match strings that optionally starts with " _ "
- Should then continue with one or more case-insensitive alphabet
- Should then contain an optional " _ " or " - " followed by case-insensitive alphanumeric characters (This step should match at lease once).
- Then it may optionally end with " _ "

The above requirements are not a problem 'cos I achieved that with:

/_?[A-Za-z]([-_]?[A-Za-z0-9])+_?/

The above RegEx matches:

  • user_
  • usEr-nAme
  • uSer_naMe-Two_
  • _Userna-Me_tHree-and-four

THE PROBLEM
How do I make it match only 6 to 25 characters?

I Tried

/(_?[A-Za-z]([-_]?[A-Za-z0-9])+_?){6,25}/

but it still matches string less than 6 like Use-r

1 Answer 1

4

Put a lookahead assertion at the start:

(?=^.{6,25}$)_?[A-Za-z]([-_]?[A-Za-z0-9])+_?

Demo here.

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

3 Comments

I guess you want: (?=.{6,25}$)?
and optional also ^ at the start or \b, not exactly sure what OP wants.
Thank you! Just what I needed!

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.