Split url string based on known start and end, with optional unknown in between segments

Trying to split strings with a known starting segment and optional additional segments until a common endpoint.

eg.

https://example.com/folder/upload/v1/subfolder/asset.doc

https://example.com/folder/upload/temp_cache_folder_id/v1/subfolder/asset.doc

Basically need to remove anything between /upload/ and /v1/

Currently got as far as

string.split(/.*?(?=v1)/)

Not sure how to proceed looking back until /upload/ and return

[https://example.com/folder , v1/subfolder/asset.doc]

upload/temp_cache_folder_id is joined with certain env variables later so it isn’t needed. That’s why the split was done in the first place.

31 thoughts on “Split url string based on known start and end, with optional unknown in between segments”

Leave a Comment