ForKoding
ForKoding

Reputation: 31

How to match if text contain the same char only n times

I'm making an API for my website and know i want to create a file load API and i want my path regex to only match this pattern

"/load/ js or css /filename"

i was able to do it with this regex

\/load\/(js|css)\/(.+)

but the problem is the regex match this as well

"/load/js/filename/anythingelse"

I don't want any / after the file name

Upvotes: 2

Views: 51

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

Since . matches all characters, you need something more specific. For example, if you want anything except /, use character groups with exclusion, i.e. [^<chars-to-exclude>]:

\/load\/(js|css)\/([^/]+)

I don't want to get the file name if there is any / after it i don't want the regex to match at all

Add $ at the end to prevent matches other than at the end of the string:

\/load\/(js|css)\/([^/]+)$

For completeness, if you are looking for full-string matches, add ^ anchor at the beginning of your regex:

^\/load\/(js|css)\/([^/]+)$

Upvotes: 1

Related Questions