我需要检查文件名后缀是否为JavaScript类型(JS文件以*.js结尾) 为此,我使用了以下有效的代码
var ext = aa.getName().substr(aa.getName().lastIndexOf('.') + 1);
现在的问题是,如果文件名为 file2.json
我仍然是真的(它返回 json)
我的问题是是否有更好的方法来做到这一点,即给定任何文件名
file1.xml
, file2.js
, file3.json
or file4.html
, 返回true 文件2
。
最佳答案
我相信这是可行的
function check(str){
if(str.match(/(\w*)\.js$/) == null){
console.log('false');
return false;
}
else {
console.log('true');
return true;
}
}
check('file1.xml');
check('file2.js');
check('file3.json');
check('file4.html');
关于javascript - 检查文件名后缀是否为 .js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45532761/