如何使用javascript更改文件扩展名 [英] How do I change file extension with javascript

查看:152
本文介绍了如何使用javascript更改文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

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

This will change the string containing the file name;

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

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

For situations where there may not be an extension:

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

这篇关于如何使用javascript更改文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆