Regex testing courtesy of
Online regex tester and debugger: JavaScript, Python, PHP, and PCRE which is awesome because it describes your regex:
/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu.be\/)([A-Z0-9._%-]*)[&w;=+_-]*/gi
(?:https?:\/\/)? Non-capturing group
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
http matches the characters http literally (case insensitive)
s? matches the character s literally (case insensitive)
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
: matches the character : literally
\/ matches the character / literally
\/ matches the character / literally
(?:www\.)? Non-capturing group
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
www matches the characters www literally (case insensitive)
\. matches the character . literally
(?:youtube\.com\/watch\?v=|youtu.be\/) Non-capturing group
1st Alternative: youtube\.com\/watch\?v=
youtube matches the characters youtube literally (case insensitive)
\. matches the character . literally
com matches the characters com literally (case insensitive)
\/ matches the character / literally
watch matches the characters watch literally (case insensitive)
\? matches the character ? literally
v= matches the characters v= literally (case insensitive)
2nd Alternative: youtu.be\/
youtu matches the characters youtu literally (case insensitive)
. matches any character (except newline)
be matches the characters be literally (case insensitive)
\/ matches the character / literally
1st Capturing group ([A-Z0-9._%-]*)
[A-Z0-9._%-]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
A-Z a single character in the range between A and Z (case insensitive)
0-9 a single character in the range between 0 and 9
._%- a single character in the list ._%- literally
[&w;=+_-]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
&w;=+_- a single character in the list &w;=+_- literally (case insensitive)
g modifier: global. All matches (don't return on first match)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])