I am trying to extract specific parts of a URL that follow a certain pattern like the one below:
http://example.com/somepath/this-is-composed-of-hypens-3144/someotherpath
Specifically, I am interested in extracting the portion of the URL that consists of hyphens and ends with numbers. In the given example URL, I want to extract this-is-composed-of-hypens-3144
. My current approach involves using the regex pattern below:
const re = /[a-z]*-[a-z]*-[0-9]*/gis;
const match = re.exec(url);
return (match && match.length) ? match[0] : null;
However, this approach only works if there are two hyphens in the URL. In reality, the number of hyphens can vary. How can I modify my regex pattern to accommodate URLs with a variable number of hyphens?