Get the YouTube video ID from a URL using JavaScript

Given a Youtube Video URL, The job is to get the Video ID from the URL using JavaScript. Here are few methods discussed.

  • split() method:
    This method is used to split a string into an array of substrings, and returns the new array.
    Syntax:

    string.split(separator, limit)
    

    Parameters:

    • separator: This parameter is optional. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item)
    • limit: This parameter is optional. It specifies the integer that specifies the number of splits, items beyond the split limit will be excluded from the array.

    Return value:
    Returns a new Array, having the splitted items.

  • JavaScript String substring() Method:
    This method gets the characters from a string, between two defined indices, and returns the new substring.
    This method gets the characters in a string between “start” and “end”, excluding “end” itself.
    Syntax:

    string.substring(start, end)
    

    Parameters:

    • start: This parameter is required. It specifies the position from where to start the extraction. Index of first character starts from 0.
    • end: This parameter is optional. It specifies the position (including) where to stop the extraction. If not used, it extracts the whole string.

    Return value:
    it returns a new string containing the extracted characters.

Example 1: This example gets the Video ID by RegExp.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript
      | Get the YouTube video ID from a URL.
    </title>
</head>
  
<body style="text-align:center;" 
      id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px; 
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color:green;
              font-size: 20px; 
              font-weight: bold;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var url = 'https://youtu.be/BnJWi0E3Mv0';
        up.innerHTML = "URL = " + url;
        var down = document.getElementById('GFG_DOWN');
  
        function GFG_Fun() {
            VID_REGEX =
/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
            down.innerHTML = "ID = "
            + url.match(VID_REGEX)[1];
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: This example first splits the URL and then take a portion of the string by using split() and substring() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript 
      | Get the YouTube video ID from a URL.
    </title>
</head>
  
<body style="text-align:center;"
      id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px;
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color:green;
              font-size: 20px;
              font-weight: bold;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var url = 'https://www.youtube.com/watch?v=da9gTKWIivA';
        up.innerHTML = "URL = " + url;
        var down = document.getElementById('GFG_DOWN');
  
        function GFG_Fun() {
            down.innerHTML = "ID = " 
            + url.split("v=")[1].substring(0, 11);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.