1

I have pattern:

/^(?:{?[a-z0-9\-_]+\??}?\/)+$/

Now these strings match the pattern

user/1/modify/
user/{id}/modify/
user/{id?}/modify/

It's OK, but now this pattern match

user/{id?/modify/

If the left curly brace start it must end with right. I tried with lookahead but i dont't know it is right way.

2 Answers 2

2

This should work:

^(?:(?:{\w+\??}|\w+)\/)+$

https://regex101.com/r/yO3pA5/1

It tries to match anything in the set [A-Za-z0-9_] followed by optional ? inside {}s, and then it tries to match the same set without the {}s (and optional ?, but you can add that back in), followed by a /. That way it won't allow for unclosed braces.

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

1 Comment

The easiest way is the best :)
1

You can add a positive lookahead to the first conditional match to ensure the closing bracket is there:

^(?:(?:{(?=[\w-]+\??}))?[\w-]+\??}?\/)+$

([\w-] is equivalent to [a-z0-9\-_])

See it in action

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.