As I work with JavaScript regex, my goal is to extract the image name and extension as a capture group of CSS properties.
Criteria
- Must start with "url"
- Followed by brackets
- Optional quotes inside brackets
- The location can include path information
- Must end with jpeg, jpg, gif, or png
For example:
behavior: url(#default#VML); -> Wrong ending, so ignored
background-image: url(dog.ttf); -> Wrong ending, so ignored
background-image: url('cat.png'); -> cat.png
background-image: url(bird.gif); -> bird.gif
background-image: url('../monkey.png'); -> monkey.png
background-image: url('../../rab$bit.png'); -> rab$bit.png
background-image: url('../animal/cow.jpg'); -> cow.jpg
This is what I have come up with so far:
url(?:\(\"|\(\'|\(\/?.*\/|\()(\.+)?(\/.*\/)?(\w*)+(.png|.jpg|.gif|.jpeg)
https://i.sstatic.net/6kXH5.png
https://regex101.com/r/3mMdTI/6
Unfortunately, due to the '\w' group, this regex breaks when a filename includes digits or special characters like $. Can anyone suggest a better solution?