Check if the string contains a valid Vimeo URL using JavaScript
In this article, we will show you how to check if the string contains a valid Vimeo URL using JavaScript.
We will also try to get more information from the URL such as video id, thumb image, author name, duration, description, etc.
Checkout more articles on JavaScript
Here we will discuss the two different ways to check the vimeo URL.
Ways to check the vimeo URL in JavaScript
1. Using regular expression
Use the following Regular Expression to check the vimeo URL.
1 2 3 4 5 6 | function validateVimeoURL(url) { return /^(http\:\/\/|https\:\/\/)?(www\.)?(vimeo\.com\/)([0-9]+)$/.test(url); } const videoURL = 'https://vimeo.com/133693532'; validateVimeoURL(videoURL); // Output: true |
2. Using oEmbed API
Don’t rely on Regex as Vimeo tends to change/update its URL pattern every now and then. Instead of the regular expression, we can use the oEmbed API to check the validity and get the video information.
In the following code, we will call an API to get the data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const videoURL = 'https://vimeo.com/133693532'; fetch(`https://vimeo.com/api/oembed.json?url=${videoURL}`) .then(response => { if (!response.ok) { // Invalid URL throw new Error('Invalid URL'); } else { // valid URL console.log(response); } }) .catch(err => { // Invalid URL console.error(err); }); |
I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂