Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中拆分复杂的(?)字符串_Javascript - Fatal编程技术网

在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);
   }
}