Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get only the image filename from a url with regex

I heard that there are two persons in the world: Those who know how to regex and those who will never know. I think I'm the type 2 :-/

I have this url:

http://myserver.com/_images/1-102332-2234.jpg?w=800&h=400&c=1 

And I need an regex which extracts only the filename 1-102332-2234.jpg

The file ending ca be everything with a . and the parameter "?w=..." is not necessary.

We are trying to get it to work for 2 days now... any help would be appreciated!

like image 292
Nico Avatar asked Jan 31 '26 21:01

Nico


1 Answers

try this regex

/[\w-]+\.jpg/g

and to support more file extensions

/[\w-]+\.(jpg|png|txt)/g

var output = "http://myserver.com/_images/1-102332-2234.jpg?w=800&h=400&c=1".match(/[\w-]+\.jpg/g);

console.log(output);

//covering other file extensions
var output = "http://myserver.com/_images/1-102332-2234.jpg?w=800&h=400&c=1".match(/[\w-]+\.(jpg|png|txt)/g);

console.log(output);
like image 120
gurvinder372 Avatar answered Feb 02 '26 10:02

gurvinder372