Skip to content
Related Articles

Related Articles

Improve Article
Get the YouTube video ID from a URL using JavaScript
  • Last Updated : 31 May, 2019

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.




<!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>

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.




<!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>

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :