gpt4 book ai didi

javascript - 如何使用 javascript 更改文件扩展名

转载 作者:行者123 更新时间:2023-12-02 07:55:46 26 4
gpt4 key购买 nike

有人知道在 Javascript 中更改文件扩展名的简单方法吗?

例如,我有一个带有“first.docx”的变量,但我需要将其更改为“first.html”。

最佳答案

这将更改包含文件名的字符串

let file = "first.docx";

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

对于可能没有扩展的情况:

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

关于javascript - 如何使用 javascript 更改文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5953239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com