1

I know how to validate a constant string with regex, but I'm really having trouble finding out how to do the following: I want a regex to validate the string edv_ after that I want the validation to be dependent:

  • if the user inputs for example edv_2, only 6 or 7 can be the next character. So only edv_26 or edv_27 would be valid
  • if the user would enter edv_3 then only edv_32 or edv_39 would be valid

Ive tried searching on the internet watched several youtube tutorials. None of them seem to handle this kind of thing. It's always only 1 constant thing they want to validate.

/[e][d][v][_][A]/ig

This matches the first part (edv_digit) but I have no clue how I should continue with the if else conditions.

1 Answer 1

2

You basically need alternations for handling your various cases. You can use this regex which matches as per your criteria.

\bedv_(?:2[67]|3[29])\b

Here boundaries ensure it doesn't match partial text like abcedv_26 or edv_26111 and it starts matching with edv_ then looks for either 2 followed by either 6 or 7 or looks for 3 followed by 2 or 9.

Live Demo

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.