-4

I am making a little youtube app but there is one problem. I get 2 kind of youtube url's and both are not able to embed. So i have to change them. Now my logic is to extract the youtube video code out of the URL. The 2 links i get are as follows:

https://youtu.be/TbvMgLDVUd4 -> i need the TbvMgLDVUd4

https://www.youtube.com/watch?v=72yuprTXvMI-> i need the 72yuprTXvMI

So my idea is something like this but i have no idea how to exactly make this...

if (videos.results[i].titlemay_link === https://www.youtube.com/watch?v=.........) {
       var output = only get the ..........
      } else {
        var output = only get the ..........
      }

      content += '<iframe width="560" height="315" src="' + outcome + '" frameborder="0" allowfullscreen></iframe>';
  • 1
    Possible duplicate of [Get everything after the dash in a string in javascript](http://stackoverflow.com/questions/573145/get-everything-after-the-dash-in-a-string-in-javascript) – TRose Dec 27 '15 at 15:58
  • RegEx is your friend if the links can get more complex, otherwise you can solve this with a simple replace. – Shomz Dec 27 '15 at 16:01

1 Answers1

1

Check if the string contains ?v= and if it does, get the content after that. If not, get the content after the last occurrence of /.

var token;
if(url.indexOf("?v=") > -1){
  // Create an array using "?v=" as a delimiter, and we want the content on the right side of the original string
  token = url.split("?v=")[1];
}else{
  // Create an array using "/" as a delimiter, and pop() to get the last element in the array
  token = url.split("/").pop();
}
Nick Zuber
  • 5,467
  • 3
  • 24
  • 48