Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将多个youtube/vimeo链接转换为嵌入式播放器_Javascript_Jquery_Regex_Youtube - Fatal编程技术网

Javascript 将多个youtube/vimeo链接转换为嵌入式播放器

Javascript 将多个youtube/vimeo链接转换为嵌入式播放器,javascript,jquery,regex,youtube,Javascript,Jquery,Regex,Youtube,因此,我试图将多个链接转换为youtube/vimeo嵌入iFrame 它似乎适用于messageText div中的单个视频,但当我添加多个视频时,链接中断 看着我!!!youtube.com/watch?v=8tv-e9DJqK4 youtube.com/watch?v=8tv-e9DJqK4 youtube.com/watch?v=8tv-e9DJqK4 $(文档).ready(函数(){ $('.messageText').html(函数(i,html){ address=html.

因此,我试图将多个链接转换为youtube/vimeo嵌入iFrame

它似乎适用于messageText div中的单个视频,但当我添加多个视频时,链接中断


看着我!!!youtube.com/watch?v=8tv-e9DJqK4 youtube.com/watch?v=8tv-e9DJqK4 youtube.com/watch?v=8tv-e9DJqK4
$(文档).ready(函数(){
$('.messageText').html(函数(i,html){
address=html.replace(/(?:http:\/\/;https:\/\/)(?:www\)(?:youtube\.com | youtu\.be)\/(?:watch\?v=)(.+)/g',)。replace(/(?:http:\/\/)(?:www\)(?:vimeo\.com)\/(.+)/g',);
警报(地址);
回信地址;
});
});

正如我所说,我正在尝试将任何youtube、vimeo链接转换为嵌入式播放器,无论div中有多少视频。提前感谢您的帮助

实际上,您使用的是贪婪运算符,您捕获的内容不正确,对正则表达式稍加修改即可:

用这个

(?:http:\/\/|https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.*?)([^\s]+)
然而,你实际上不需要纠正这么长的正则表达式,你可以用lookaheads代替。就目前而言,这将对你有效

同样,您也可以为vimeo计算:)

更新小提琴:

干杯

html.replace(/(?:http:\/\/\/;https:\/\/)(?:www\)(?:youtube\.com | youtu\.be)\/(?:watch\?v=)(.*)([^\s]+)/g.)。replace(/(?:http:\/\/\/;https:\/\/)(?:www\)(?:vimeo\.com)\/(.+)/g.);
html.replace(/(?:http:\/\/|https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.*?)([^\s]+)/g, '<iframe src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen id="videoPlayer" ></iframe>').replace(/(?:http:\/\/|https:\/\/)?(?:www\.)?(?:vimeo\.com)\/(.+)/g, '<iframe id="videoPlayer" src="//player.vimeo.com/video/$1" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>');

谢谢@aelor,我是def。更干净!我不太熟悉regex,这就是原因。@ThelsonRichardson你可能想快速学习regex,这里有一个网站帮助我学习Regexonnewon,如果url是“html插入的”,那就不好了,比如
youtube.com/watch?v=8tv-e9DJqK4

html.replace(/(?:http:\/\/|https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.*?)([^\s]+)/g, '<iframe src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen id="videoPlayer" ></iframe>').replace(/(?:http:\/\/|https:\/\/)?(?:www\.)?(?:vimeo\.com)\/(.+)/g, '<iframe id="videoPlayer" src="//player.vimeo.com/video/$1" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>');