Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create javascript RegEx that will extract image name from url

I hope you can help me create a regex that will extract the image name from a url. I've been trying to create the regex for a couple of days now but i cant get it to work.

The case: I have an image url created with phpThumb that looks like this: \bla\thumb\phpThumb.php?src=/bla/images/thisistheimage.jpg&fltr[]=wmt|40|B|FFFFFF|georgia.ttf|35&hash=b7412f04f09cd6b488435231651d61453

No i need the following extracted: thisistheimage.jpg

Some facts:

  • there is always a "/" in front of the image name;
  • the image name is always followed by an amp (&);
  • the image extention can also be: jpeg, JPG, png, PNG, gif;

All i have managed to grab was the image extension with (?:png|jpg|gif).

Can you please help me what regex I have to use in this case?

I'm able to use the regex in javascript or in PHP, where do you recommend it? (I use javascript to submit the form and the form action is done in PHP).

like image 420
user1732556 Avatar asked May 29 '26 01:05

user1732556


1 Answers

/\/([A-Z0-9_-]{1,}\.(?:png|jpg|gif|jpeg))/i

I think that should work. i flag for insensitive case

  • matches slash /
  • Alphanumeric, , and - one or more [A-Z0-9-]{1,}
  • period .
  • extension (?:png|jpg|gif|jpeg)
like image 188
James L. Avatar answered May 30 '26 15:05

James L.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!