Youtube I.D parsing for new URL formats

前端 未结 5 1824
借酒劲吻你
借酒劲吻你 2020-12-02 12:18

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

5条回答
  •  情书的邮戳
    2020-12-02 12:58

    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

提交回复
热议问题