preg_match正则表达式部分因youtube网址而失败

var_dump(preg_match("/^((http\:\/\/){0,}(www\.){0,}(youtube\.com){1}|(youtu\.be){1}(\/watch\?v\=[^\s]){1})$/", "http://www.youtube.com/watch?v=M7FIvfx5J10")); This code will return 0 as it failed.. why? What is wrong in regex? You can test it here http://writecodeonline.com/php/

Try this :

var_dump(preg_match("/^((http\:\/\/){0,}(www\.){0,}((youtube\.com){1}|(youtu\.be){1})\/watch\?v\=[^\s]*)$/", "http://www.youtube.com/watch?v=M7FIvfx5J10"));

It seems to work. You forgot the quantifier for the video id and parenthesis around the OR for youtube.com / youtu.be