Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for filename in img src attribute

I have url and img tag like these (files can have gif, png or jpg extension):

'C:\fakepath\Chrysanthemum.jpg'
'<img src="http://localhost/rootil/uploads/arrow.jpg">'

and I want to parse filenames from them, so I get

'Chrysanthemum.jpg'
'arrow.jpg'

respectively.

My first try is:

alert(g.slaves[f].value.match(/\.\w{1,4}$/gi))

but it gives me only .jpg.

Can someone help me with correct regexp for that situation?

like image 914
Kate Wintz Avatar asked Oct 15 '25 06:10

Kate Wintz


1 Answers

I think that should work for you:

var r = /[^/\\]+(?:jpg|gif|png)/gi;

var s = 'C:\\fakepath\\Chrysanthemum.jpg \
         <img src="http://localhost/rootil/uploads/arrow.jpg">';

s.match(r); 
// => ["Chrysanthemum.jpg", "arrow.jpg"]
like image 165
KL-7 Avatar answered Oct 16 '25 22:10

KL-7