Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.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
I';我使用JavaScript&;Regex可以围绕链接构建对象。需要澄清一下吗_Javascript_Jquery_Html_Regex_Youtube - Fatal编程技术网

I';我使用JavaScript&;Regex可以围绕链接构建对象。需要澄清一下吗

I';我使用JavaScript&;Regex可以围绕链接构建对象。需要澄清一下吗,javascript,jquery,html,regex,youtube,Javascript,Jquery,Html,Regex,Youtube,为了澄清这一点,我构建了一个注释系统,它可以清理所有HTML,并将其显示为纯文本,以防止拖拉、跨站点脚本等 最重要的是,我有javascript在页面加载后运行,检测到Youtube和Imgur内容的直接链接,然后构建适当的播放器/框架/标记来显示该内容 下面是我的示例代码: <div class="video imgur"> https://i.imgur.com/Ym7MypF.jpg https://www.youtube.com/watch?v=t-ZRX8984sc &l

为了澄清这一点,我构建了一个注释系统,它可以清理所有HTML,并将其显示为纯文本,以防止拖拉、跨站点脚本等

最重要的是,我有javascript在页面加载后运行,检测到Youtube和Imgur内容的直接链接,然后构建适当的播放器/框架/标记来显示该内容

下面是我的示例代码:

<div class="video imgur">
https://i.imgur.com/Ym7MypF.jpg

https://www.youtube.com/watch?v=t-ZRX8984sc
</div>

https://i.imgur.com/Ym7MypF.jpg
https://www.youtube.com/watch?v=t-ZRX8984sc
和脚本:

$('.video').html(function(i, html) {
    return html.replace(/(?:https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g, '<iframe width="420" height="345" src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>');
});

$('.imgur').html(function(i, html) {
    return html.replace(/(?:https:\/\/)?(?:i\.)?(?:imgur\.com|)\/(.+)/g, '<img src="https://i.imgur.com/$1">');

});
$('.video').html(函数(i,html){
返回html.replace(/(?:https:\/\/)(?:www\)(?:youtube\.com | youtu\.be)\/(?:watch\?v=)(.+)/g',);
});
$('.imgur').html(函数(i,html){
返回html.replace(/(?:https:\/\/)(?:i\)(?:imgur\.com |)\/(.+)/g',);
});
我可以让其中一个在没有另一个的情况下工作,但是,在同一页面上运行这两个页面总是会产生像这样的断开的标记和链接,具体取决于顺序:

<iframe width="420" height="345" src="https:&lt;img src=" https:="" i.imgur.com="" www.youtube.com="" embed="" t-zrx8984sc"="" frameborder="0" allowfullscreen=""></iframe>

您的Imgur正则表达式匹配的URL太多,例如:


试着改用这个正则表达式:
/(?:https:\/\/)(?:i\.)(?:imgur\.com)\/(.+)/g

这很快就奏效了,而且在几个测试注释中都起到了作用。非常感谢。