Sunday, May 12, 2024
142
rated 0 times [  143] [ 1]  / answers: 1 / hits: 57146  / 13 Years ago, tue, may 10, 2011, 12:00:00

Does anyone know an easy way to change a file extension in Javascript?



For example, I have a variable with first.docx but I need to change it to first.html.


More From » programming-languages

 Answers
5

This will change the string containing the file name;


let file = "first.docx";

file = file.substr(0, file.lastIndexOf(".")) + ".htm";

For situations where there may not be an extension:


let pos = file.lastIndexOf(".");
file = file.substr(0, pos < 0 ? file.length : pos) + ".htm";

[#92303] Monday, May 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;