在javascript中拆分复杂的(?)字符串
我得到了这个字符串:在javascript中拆分复杂的(?)字符串,javascript,Javascript,我得到了这个字符串: my.song.mp3 greatSong.flac not3.txt video.mp4 game.exe mov!e.mkv 我需要从文件类型中拆分文件名 考虑到“.”与.mp3文件一样存在问题,我该如何做 从字符串中最后一次出现的“.”处修剪它 let fileName=“my.song.mp3”; fileName=fileName.substring(0,fileName.lastIndexOf(“.”) console.log(文件名)请指定您的要求。你到底
my.song.mp3 greatSong.flac not3.txt video.mp4 game.exe mov!e.mkv
我需要从文件类型中拆分文件名
考虑到“.”与.mp3文件一样存在问题,我该如何做 从字符串中最后一次出现的“.”处修剪它
let fileName=“my.song.mp3”;
fileName=fileName.substring(0,fileName.lastIndexOf(“.”)
console.log(文件名)
请指定您的要求。你到底想要什么。
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);
}
}