Advertisement
Xtremrules

Youtube Regex

May 21st, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. This is the regular expression
  2. ^(http(s)??\:\/\/)?(www\.|m\.)?((youtube\.com\/watch\?v=)|(youtu.be\/))([a-zA-Z0-9\-_]+)$
  3. This could also work since all youtube video id is 11 character
  4. ^(http(s)??\:\/\/)?(www\.|m\.)?((youtube\.com\/watch\?v=)|(youtu.be\/))([a-zA-Z0-9\-_]{11})$
  5.  
  6. It matches the following
  7. https://www.youtube.com/watch?v=DFYRQ_zQ-gk
  8. http://www.youtube.com/watch?v=DFYRQ_zQ-gk
  9. www.youtube.com/watch?v=DFYRQ_zQ-gk
  10. https://youtube.com/watch?v=DFYRQ_zQ-gk
  11. http://youtube.com/watch?v=DFYRQ_zQ-gk
  12. youtube.com/watch?v=DFYRQ_zQ-gk
  13. https://m.youtube.com/watch?v=DFYRQ_zQ-gk
  14. http://m.youtube.com/watch?v=DFYRQ_zQ-gk
  15. m.youtube.com/watch?v=DFYRQ_zQ-gk
  16. https://youtu.be/DFYRQ_zQ-gk
  17. http://youtu.be/DFYRQ_zQ-gk
  18. youtu.be/DFYRQ_zQ-gk
  19.  
  20. Note.
  21. Get the video id and create yours.
  22. try this
  23.  
  24. function extractVideoID(url){
  25. var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
  26. var match = url.match(regExp);
  27. if ( match && match[7].length == 11 ){
  28. return match[7];
  29. }else{
  30. alert("Could not extract video ID.");
  31. }
  32. }
  33.  
  34. https://ctrlq.org/code/19797-regex-youtube-id
  35.  
  36. Remember to match before editing: https://regexr.com/3ppe2
  37.  
  38. Use the following link to get the img of a youtube video
  39. https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api
  40.  
  41. Each YouTube video has 4 generated images. They are predictably formatted as follows:
  42.  
  43. https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
  44. https://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
  45. https://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
  46. https://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
  47. The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg, 2.jpg, 3.jpg) is:
  48.  
  49. https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
  50. For the high quality version of the thumbnail use a url similar to this:
  51.  
  52. https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
  53. There is also a medium quality version of the thumbnail, using a url similar to the HQ:
  54.  
  55. https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
  56. For the standard definition version of the thumbnail, use a url similar to this:
  57.  
  58. https://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
  59. For the maximum resolution version of the thumbnail use a url similar to this:
  60.  
  61. https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
  62. All of the above urls are available over http too. Additionally, the slightly shorter hostname i3.ytimg.com works in place of img.youtube.com in the example urls above.
  63.  
  64. Alternatively, you can use the YouTube Data API (v3) to get thumbnail images.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement