Yes: jsperf. However, it is a micro-optimization. So, if you do this only once in your code, take whatever solution you want. If you do this times every second e. I mean people, not IE users.
Show 1 more comment. Amber Amber k 78 78 gold badges silver badges bronze badges. Also think about things like a. Actually, just use pathinfo. An almost perfect solution but, if you want to avoid the trap of filenames like. Roger Pate Roger Pate. Igor Igor That regex is returning the extension, whereas I need to remove the extension — Andreas Grech. AndreasGrech well, regexes don't remove things. They match things. If you use a program like SED, then to remove it you match it and replace it with empty string.
Of course other option is to match all that is not the extension. This is the only answer that worked with. It is a simple and reasonable solution, but it fails in some exceptions given by Roger Pate, e. Steven Pribilinskiy Steven Pribilinskiy 1, 16 16 silver badges 21 21 bronze badges. Daniel Daniel 27k 15 15 gold badges 82 82 silver badges bronze badges. Thanks a lot for this tutorial. May 15, at AM. Muhammad Said Antony Said December 4, at AM. CodexWorld Said Yes, it will work.
December 5, at PM. Amit Shee Said Thanks for sharing this tutorial. June 11, at AM. Anikul Said Add a comment. Active Oldest Votes. Improve this answer. Always Learning Always Learning 5, 2 2 gold badges 14 14 silver badges 32 32 bronze badges.
It does work.. Apoorv Apoorv 7 7 silver badges 20 20 bronze badges. You can use fileReader in js. In fileName you get only file name. Iiskaandar Iiskaandar 2 2 silver badges 7 7 bronze badges.
Pretty much the same as stackoverflow. And unless you do one heck of a lot of these, worrying about efficiency is Premature Optimisation. In the age of ES6, also see the Path module — in case you are using nodejs or a proper transpilation — Frank Nocke. Add a comment. Active Oldest Votes. Improve this answer. Mosh Feu John Hartsock John Hartsock This works for any length of file extension.
It wouldn't handle eg. It's more common for a file name to contain additional periods than a file extension. Vik There's a difference between the 'right answer' and the accepted answer.
An accepted answer is just the answer that was helpful for the one who asked the question. I suppose that there may be issues with the Windows platform because there can be back slashes.
ElgsQianChen here is a great tool for you to help answer your question regexr. Show 5 more comments. Viacheslav Dobromyslov 2, 29 29 silver badges 42 42 bronze badges. Jibesh Patra Jibesh Patra 4, 1 1 gold badge 13 13 silver badges 14 14 bronze badges. This answer is pretty restricted to server-side node. If you try to use this in react code, it doesn't seem to import.
If you'd rather not use regular expressions, you can try this less performant : filename. Arghya 12 12 bronze badges. Marek Sapota Marek Sapota I like this solution the best.
It's clean, and I can use it cause I know the file extension is always. I was looking for something like Ruby's x[ And thank you to everyone else for all the other robust alternatives provided! This is dangerous solution, cause you don't really know the length of the format. Don't use this anymore. Show 1 more comment. EDIT: To answer Jeff B Jeff B You can have a filename ex: "summer.
0コメント