首页 > 解决方案 > 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!

标签: javascriptregex

解决方案


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


推荐阅读