I have created a simple Chrome extension for personal use and sharing with friends. I am dealing with URLs that have various formats, such as:
i.imgur.com/abcd123.png
or
imgur.com/a2b3c78
or even
i.imgur.com/herp321.png?1
All I need from these URLs are the 7-character codes before the file extensions. For example, abcd123
, a2b3c78
, and herp321
.
I have tried using .replace but it doesn't handle exceptions like the third example. I also attempted to use regex, but couldn't get it to work properly. The only constant part of the URL is the following format, with the code represented as x
:
imgur.com/xxxxxxxx
The beginning and end of the URL may vary, but the above format remains consistent. Is there a way to extract just the alphanumeric filename from the examples provided, regardless of what comes before the URL or any additional modifications/extensions?