user8551460
user8551460

Reputation:

JS regex to match all parts of the URL ignoring a specific path that consists of a dynamic value within that URL

Example URL that the regex should match:

https://domain/a/b/c/d/e/i39m33rgp5jcrohl5atwe4c9/g/h.file

This shouldn't be matched: i39m33rgp5jcrohl5atwe4c9

I was thinking something along the lines of this, tests on https://regex101.com/ doesn't seem to match it properly though. Any pointers?

^/a/([^/]+/)?([^/]+/)?([^/]+/)?([^/]+/)?([^/]+/)?([^/]+/)(\.file)?$

Upvotes: 1

Views: 130

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133760

With your shown samples, could you please try following.

(^https?:\/\/(?:.*?\/){6}).*?\/(.*)$

Online demo for above regex

Explanation: Adding detailed explanation for above.

(               ##Starting 1st capturing group here.
  ^https?:\/\/  ##Checking condition if value starts from http/https://
  (?:.*?\/){6}  ##In a non-capturing group doing non-greedy match till / up to 6 times, just before dynamic value in URL.
)               ##Closing 1st capturing group here.
.*?\/           ##Again going non-greedy match till next occurrence of / here.
(.*)$           ##In 2nd capturing group having everything, rest of URL here.

Upvotes: 2

Related Questions