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


source to share


3 answers


You can try the following regex,

\/\/(.*\/){3}

      



Working demo

+1


source


Use a lookahead like:

^(?=(?:[^\/]*\/){4,})(.*)

      



Demo

0


source


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


source







All Articles