How to modify a regular expression to parse a para

2019-01-29 03:21发布

I've got this regular expression:

/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;

It will parse the video id from a YouTube URL such as

http://www.youtube.com/watch?v=6ciG_6Y3rD0&feature=youtu.be&t=6m40s

or

http://youtu.be/6ciG_6Y3rD0?t=6m40s

I want to modify it so that it also parses the t parameter.

Any thoughts?

Thanks, Howie

1条回答
你好瞎i
2楼-- · 2019-01-29 04:12

Capture YouTube video ID (and time seek parameter t if exists)

RegExp:

^(?:https?\:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v\=))([\w-]{10,12})(?:[\&\?\#].*?)*?(?:[\&\?\#]t=([\dhm]+s))?$

Demo here: http://fiddle.re/w1nn6

PS: you'll note I improved your original RegEx to validate the YouTube link safely

查看更多
登录 后发表回答