简体   繁体   中英

how to determine whether that extension is for an audio type?

In JavaScript, I have a string contain filename + ext like: var s = "blabla.mp3" .

How to find out if the string contain file ext that matches for audio type? mp3, mp4....

var s = 'blabla.mp3';
function isAudioType(s) { return true/false; }

You can using regex:

function isAudioType(s) { 
  return /\.(mp3|mp4)$/i.test(s);
}

Use a function which matches the target file name's extension against a list of desired extensions you want to check.

 function isAudioType(s) { var audioTypes = [".mp3", ".wav"], // Add as many extensions you like here... audioExt = s.replace(/^.+(?=\\.)/i, ''); return (audioTypes.indexOf(audioExt.toLowerCase()) > -1); } console.log(isAudioType('blabla.mp3')); console.log(isAudioType('blabla.jpg')); 

With this code you can add as many cases as you want. In this example I added the mp3 case only. You could add mp4 and others too, using a switch for example.

var s = 'blabla.mp3';

function isAudio (fileName) {
    let arr = fileName.split('.');

    if (arr[arr.length-1] === 'mp3'){
        return true;
    }

}

console.log(isAudio(s));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM