javascriptregexmountebank

Regex Pattern Matching for similar URL


I need to send different Response for different urls. But the Regex that I am using is not working.

The two Regex in question is

"/v1/users/[^/]+/permissions/domain/HTTP/"    

(Eg: http://localhost:4544/v1/users/10feec20-afd9-46a0-a3fc-9b2f18c1d363/permissions/domain/HTTP)

and

"/v1/users/[^/]+/"    

(Eg: http://localhost:4544/v1/users/10feec20-afd9-46a0-a3fc-9b2f18c1d363)

I am not able to figure out how to stop the regex matching after "[^/]+/". Both the pattern return the same result. It is as if due to regex both of them are same URL's. The pattern matching happens in mountebank mocking server using a matching predicate. Any help would be appreciated. Thanks.


Solution

  • The regular expression "/v1/users/[^/]+/" matches both urls. You are asking it to match '/v1/users/` plus anything except '/' followed by a slash. This happens in both the longer URL and the short one, which is why it matches.

    A couple options:

    You can match the longer url and not the shorter one with:

    "/v1/users/[^/]+/.+"
    

    This matches http://localhost:4544/v1/users/10feec20-afd9-46a0-a3fc-9b2f18c1d363/permissions/domain/HTTP, but not http://localhost:4544/v1/users/10feec20-afd9-46a0-a3fc-9b2f18c1d363/

    You could also match just the short one by anchoring the end:

    "/v1/users/[^/]+/$"
    

    This matches the short URL but not the long one.