Check if the string contains a valid Vimeo URL using JavaScript
π
August 5, 2021
π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
- Get the YouTube video ID from a URL using JavaScript
- Verify an image URL in JavaScript
- Execute code only after all images have been loaded in JavaScript
- Set text direction based on input text language in JavaScript
- Methods of Promises in 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.
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.
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..!! π