This regex here will ensure that the url supplied matches the criteria of a valid youtube url:
- This is that it is from www.youtube.com or youtu.be
- That it contains either a 'v=' querystring or has the id as part of the url supplied
- It also makes sure that the id is 11 characters long.
If you're allowing customers to save YouTube videos on your site and you'd like to extract out the video id when storing into your database you could try using either of these options below.
The thing to remember is that the YouTube url can be either:
and the Video Id can be appended as a querystring parameter or as the last segment of the url: