我得到了这个字符串:
my.song.mp3 greatSong.flac not3.txt video.mp4 game.exe mov!e.mkv
我需要从文件类型中拆分文件名。
如何考虑“。”在.mp3文件中有问题吗?
答案 0 :(得分:0)
从最后一次出现的“。”开始修剪它。在字符串中。
let fileName = "my.song.mp3";
fileName = fileName.substring(0, fileName.lastIndexOf("."))
console.log(fileName)
编辑:刚刚意识到您可能是一个大字符串,需要返回一个文件名数组?是对的吗?如果是这样,它将起作用:
let initialString = "my.song.mp3 greatSong.flac not3.txt video.mp4 game.exe mov!e.mkv";
let fileNames = initialString.split(" ");
fileNames = fileNames.map(fileName => fileName = fileName.substring(0, fileName.lastIndexOf(".")));
console.log(fileNames);
答案 1 :(得分:0)
function test()
{
try
{
// start with initial data
var theExample = "my.song.mp3 greatSong.flac not3.txt video.mp4 game.exe mov!e.mkv" ;
// split in individual file names by splitting string on whitespace
var fileNames = theExample.split(/\s/) ;
// run over each fileName
var i = fileNames.length ;
while (i-- > 0)
{
// remove literal dot and trailing word characters at the end of the string; show result
alert( fileNames[i].replace(/\.[\w]+$/,"") ) ;
}
}
catch(err)
{
// show error message
alert(err.message);
}
}