Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull url file extension out of url string using javascript

How do I find the file extension of a URL using javascript? example URL:

http://www.adobe.com/products/flashplayer/include/marquee/design.swf?width=792&height=294 

I just want the 'swf' of the entire URL. I need it to find the extension if the url was also in the following format

http://www.adobe.com/products/flashplayer/include/marquee/design.swf 

Obviously this URL does not have the parameters behind it.

Anybody know?

Thanks in advance

like image 247
Carl Avatar asked Aug 09 '11 13:08

Carl


People also ask

How do I get the file extension of an input type file?

The full filename is first obtained by selecting the file input and getting its value property. This returns the filename as a string. By the help of split() method, we will split the filename into 2 parts. The first part will be the filename and the second part will be the extension of the file.


1 Answers

function get_url_extension( url ) {     return url.split(/[#?]/)[0].split('.').pop().trim(); } 

example:

get_url_extension('https://example.com/folder/file.jpg'); get_url_extension('https://example.com/fold.er/fil.e.jpg?param.eter#hash=12.345'); 

outputs ------> jpg

like image 74
T.Todua Avatar answered Oct 13 '22 19:10

T.Todua