Improving regex for parsing YouTube / Vimeo URLs

后端 未结 11 1612
半阙折子戏
半阙折子戏 2020-12-13 20:52

I\'ve made a function (in JavaScript) that takes an URL from either YouTube or Vimeo. It figures out the provider and ID for that particular video (demo: http://jsfiddle.net

11条回答
  •  长情又很酷
    2020-12-13 21:09

    about sawa's answer :

    a little update on the second regex :

    /http:\/\/(?:www\.)?(vimeo|youtube)\.com\/(?:watch\?v=)?(.*?)(?:\z|$|&)/
    

    (escaping the dots prevents from matching url of type www_vimeo_com/… and $ added…)

    here is the same idea for matching the embed urls :

    /http:\/\/(?:www\.|player\.)?(vimeo|youtube)\.com\/(?:embed\/|video\/)?(.*?)(?:\z|$|\?)/
    

提交回复
热议问题