Regex to find at least 4 occurrences of a forward slash in a string
I have some urls and I would like to make sure they have at least 4 slashes as some of my urls have less. For example:
Pass: http: // localhost: 2000 / machine / my-test-machine / 3
Failure: http: // localhost: 2000 / my-test-machine
Any help would be much appreciated. thanks to Dave
+3
dave
source
to share
3 answers
You can try the following regex,
\/\/(.*\/){3}
Working demo
+1
apgp88
source
to share
Use a lookahead like:
^(?=(?:[^\/]*\/){4,})(.*)
Demo
0
dawg
source
to share
Just compare it 4 times:
(?:.*?/){4}
Watch live demo .
The reluctance quantifier *?
ensures that trailing slashes are not skipped in matching (excluding trail tracking)
Your regex engine (undefined) may require escaping forward slashes, i.e. \/
0
Bohemian
source
to share