Get the YouTube video ID from a URL using JavaScript

Today, we will show you how to get the youtube video id from a URL using JavaScript.

We have many ways but here we will look at two different ways to get a YouTube video ID from a URL.

Two different ways to get a YouTube video ID

  1. Using regular expression
  2. Using replace and split methods

1. Using regular expression

Here we will use the regular expression to match the url and get the video id. Look at the following function to get a youtube video id.

2. Using replace and split methods

In the second method, we will use the replace and split methods to get the id. Check the following code.

Recommended Articles
Replace all occurrences of a string in JavaScript
Remove all whitespace from a string in JavaScript

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *