1

How to get file extension in path using JavaScript?

alert("http://max/fi.le.rar".match(/\..*/i));

returns data since first dot

.le.rar

but I need only .rar

3 Answers 3

5

Try this:

"http://max/fi.le.rar".match(/\.[^.]+$/);
Sign up to request clarification or add additional context in comments.

1 Comment

I should comment that the returning value is an array of string
1
var path = "http://max/fi.le.rar";
var extention = path.substring(path.lastIndexOf("."), path.length);

I did a test with substring and split from @wisdom.
After 500000 times:

.rar Execution time: 47ms (sub)
rar Execution time: 53ms (split without dot)
(after some more testing the execution times are about the same)


I have no experience with match

Comments

0

or You can use javascript split() function like this

var str="http://max/fi.le.rar";
var n=str.split(".");
alert(n[n.length - 1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.