• Post category:JavaScript
  • Reading time:2 mins read

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.

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
  2. Using oEmbed API

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

Leave a Reply