Skip to content

Instantly share code, notes, and snippets.

@jamiehs
Created October 6, 2012 14:42
Show Gist options
  • Save jamiehs/3845089 to your computer and use it in GitHub Desktop.
Save jamiehs/3845089 to your computer and use it in GitHub Desktop.
YouTube JavaScript Regex
var youTubeRegex = /(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|[a-zA-Z0-9_\-]+\?v=)([^#\&\?\n<>\'\"]*)/gi;
<!-- Different Video ID formats (random sample) -->
http://youtu.be/oHg5SJYRHA0
http://youtu.be/vOTfkVfMoec
http://youtu.be/UAqAqGMb_jQ
http://youtu.be/q4d8C7vSEdc
http://youtu.be/YmUi22qZdkA
http://youtu.be/m86Yq6vC_bY
http://youtu.be/__CR1plT1tQ
<!-- DIfferent URL formats -->
http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=feedrec_grec_index
<a title="A YouTube Video" href="http://www.youtube.com/watch?v=zV-Jjqth9jQ" target="_blank">A YouTube Video</a>
http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
<a title="A YouTube Video" href="http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o" target="_blank">A YouTube Video</a>
http://www.youtube.com/v/oHg5SJYRHA0?fs=1&amp;hl=en_US&amp;rel=0
<a title="A YouTube Video" href="http://www.youtube.com/v/oHg5SJYRHA0?fs=1&amp;hl=en_US&amp;rel=0" target="_blank">A YouTube Video</a>
http://www.youtube.com/watch?v=oHg5SJYRHA0#t=0m10s
<a title="A YouTube Video" href="http://www.youtube.com/watch?v=oHg5SJYRHA0#t=0m10s" target="_blank">A YouTube Video</a>
http://www.youtube.com/embed/oHg5SJYRHA0?rel=0
<a title="A YouTube Video" href="http://www.youtube.com/embed/oHg5SJYRHA0?rel=0" target="_blank">A YouTube Video</a>
http://www.youtube.com/watch?v=oHg5SJYRHA0
<a title="A YouTube Video" href="http://www.youtube.com/watch?v=oHg5SJYRHA0" target="_blank">A YouTube Video</a>
http://youtu.be/oHg5SJYRHA0
<a title="A YouTube Video" href="http://youtu.be/0zM3nApSvMg" target="_blank">A YouTube Video</a>
@jamiehs
Copy link
Author

jamiehs commented Oct 6, 2012

Use the HTML above to test the Regex. I'm currently using this Regex in my "Related Service Comments" plugin where I'm attempting to find YouTube URLs in the content, regardless of the format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment