Regular expression matching anything or nothing?

I am trying to make regex for embedding videos on YouTube.

Right now, this is the main job that I have:

http:\/\/www\.youtube\.com\/watch\?.*v=([a-z0-9-_]+)

It has a regular, recognized YouTube URL and then matches a unique video code. Unfortunately, this causes too many of my users. They enter it, skip HTTP, skip WWW, skip both HTTP and WWW, sometimes they enter it, replacing / watch? V = * c / v / *, and in all these scenarios it breaks the format.

What I'm trying to do is allow everything before and after "youtube.com" so that it is almost perfect, regardless of the input. Examples below ..

http://www.youtube.com/watch?v=([a-z0-9-_]+) --- the normal, unmolested input
http://youtube.com/watch?v=([a-z0-9-_]+) --- missing WWW
www.youtube.com/watch?v=([a-z0-9-_]+) --- missing HTTP
youtube.com/watch?v=([a-z0-9-_]+) --- missing HTTP and WWW
http://www.youtube.com/v/([a-z0-9-_]+) --- substituted watch?v= for /v/
http://youtube.com/v/([a-z0-9-_]+) --- substituted watch?v= for /v/ AND missing WWW 
www.youtube.com/v/([a-z0-9-_]+) --- substituted watch?v= for /v/ AND missing HTTP
youtube.com/v/([a-z0-9-_]+) --- substituted watch?v= for /v/ AND missing HTTP and WWW

This is one change that I thought it should work (allow any character), but maybe I missed something?

[.]+\youtube\.com\/[.]+([a-z0-9-_]+)

, , . , , , . , , , , , . .

, .

+3
2

...

(?:http://)?(?:www\.)?youtube\.com/(?:watch\?v=|v/)([\w-]+)

RegExr.

URL- YouTube 1.

http://, www., youtube.com/, watch?v=, v/, \w -.

+3

. , . [.]+ , , " ". - , , .+ (, , .*, "youtube" ).

0

All Articles