Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regex to get image URL in HTML/Js

Tags:

html

regex

I want to get some URLs of images in Js/HTML:

var a = "http://sub.domain.com/uploads/files/11-11-2011/345301-574-1182-393/2202.jpg";
var b = "http://sub.domain.com/uploads/files/23-11-2011/234552-574-2321-232/asd.png";

Looking for solution that will detect image url. So the output will be:

http://sub.domain.com/uploads/files/11-11-2011/345301-574-1182-393/2202.jpg
http://sub.domain.com/uploads/files/23-11-2011/234552-574-2321-232/asd.png

Thanks!

like image 740
anhtran Avatar asked Nov 04 '10 15:11

anhtran


1 Answers

Based off the information you've given, this should work:

(https?:\/\/.*\.(?:png|jpg))

You can add more extensions by adding |ext after jpg. This will allow for strings with https as well.

Note: You may want to use the case insensitive modifier i to make the capture more inclusive. This would look like:

/(https?:\/\/.*\.(?:png|jpg))/i
like image 196
tinifni Avatar answered Oct 20 '22 04:10

tinifni