I have a function written to play videos like below

  function PlayWordFile(fileNam)
        {     
            if (fileNam=="")
            {
                alert('No file attached.');
                return;
            }
            var extn = fileNam.split('.').pop();             
            if (extn == "mp4") {
                var sel=fileNam;
                var video=document.getElementById('videoPlayer');
                video.pause();
                video.removeAttribute('src'); 
                video.load();
               var source=document.createElement('source');
                source.setAttribute('src',"/x/"+sel);
                video.appendChild(source);
 video.play();     
            }
        }

the fileNam is sometimes said as "a/x.mp4" and sometimes "x.mp4" the above code will play videos with fileNam "x.mp4" can i handle both type of filename in a single function i mean if its "a/x.mp4 or x.mp4" ???

i can go with some split method or any method u can help me through this.Thanks in advance.

2 Answers

0
whiterabbitj On

Use regex to mach the pattern of the string which is : (anyString) + . + mp4

 var regex = new RegExp ("^.*\.(mp4)$")
 if (regex.test(fileNam)){          
     // logic here
    }
0
marcelo On

Here's how i get through using the indexof() method


            if (fileNam.indexOf('/')> -1) {

                var x=fileNam.split('/')[1];   
               //logic
        }