1

How can I check if a given URL points to a valid YouTube video?

Ry-
  • 218,210
  • 55
  • 464
  • 476
user1535672
  • 193
  • 3
  • 12

2 Answers2

0

Match it with a regular expression.

if($parsed[0].match(/youtube\.com/) {
    // affirmative
} else {
    // negative
}

I'm calling match() on the first element of $parsed, but your "domain" portion could very well be the 3rd element if your URL string started with "http://".

There's probably a more rock-solid regex you could use there, but that'll evaluate as true if string contains the youtube.com domain.

Edd Morgan
  • 2,873
  • 17
  • 22
0

The below function will return true or false on passing url:

function matchYoutubeUrl(url){
var p = /^(?:https?:\/\/)?(?:www\.)?    (?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
 return (url.match(p)) ? true : false ;
}
atul singh
  • 664
  • 5
  • 11