Ives E.
Ives E.

Reputation: 113

Regex: Check if (among others) specific characters are present in a string

I'm working with next.js and I have a string (url). I need to check if the string contains at least 3x "/". Other chars are also present (letters, numbers and "-") - but they don't need to match something specific.

Example:

I think I need the following snippets:

I just don't know how to put them together in a regex. Could someone help me? Thanks a lot!

Upvotes: 0

Views: 105

Answers (1)

The fourth bird
The fourth bird

Reputation: 163207

You can repeat the / followed by the accepted characters 3 or more times.

Note that [A-Za-Z0-9] should be [A-Za-z0-9] instead.

^(?:\/[A-Za-z0-9-]+){3,}

Regex demo

To not match - only, or at the start or end, you can optionally repeat it:

^(?:\/[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*){3,}

Regex demo

Upvotes: 4

Related Questions