This question has been asked before and I found this:
Reg exp for youtube link
but I\'m looking for something slightly different.
I need to match th
I don't know if this is what your looking for, but I found this great list of YouTube URLs (GitHub).
Some of the URLs in the list are for proxy services and attribution links. In my use case, the supplied string can either be a URL or just an ID - so a regex match wont do.
So, based on all of the possibilities here, the ID can be extracted with two regex replaces:
^.+(\/|vi?=|v%3D)
In all sampled cases this selects everything between the start of the string and the start of the ID. Here is a demo RegExr
[^a-zA-Z0-9_\-].+$
Additionally, in all sampled cases, this selects everything from the end of the ID (in the now truncated string) to the end of the string. Here is a demo RegExr