How do I use lookahead assertion to limit by range the number of “/”
I have tired the following
^(?=/{1,3})$
but it doesn’t work
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The easiest solution is to use a negative lookahead:
That basically means the string cannot contain 4 slashes.
This assumes you allow other characters between slashes, but a maximum of 3 slashes.
A positive version would be
^(?=[^/]*(?:/[^/]*){0,3}$)or^[^/]*(?:/[^/]*){0,3}$, without the lookahead.Of course, the problem is trivial without regular expressions, if possible.
Lets try to break that last one down:
^– Start of the string.[^/]*– Some characters that are not slashes (or none)(?: )– A logical group. Similar to(), but does not capture the result (we do not need it after validation)/[^/]*– Slash, followed by non-slash characters.{0,3}– From 0 to 3 times.$– End of the string.