相关疑难解决方法(0)

检查JavaScript字符串是否为URL

有没有办法在JavaScript中检查字符串是否是URL?

RegExes被排除在外,因为URL最有可能写成stackoverflow; 也就是说它可能没有.com,www或者http.

javascript string url

222
推荐指数
18
解决办法
26万
查看次数

Javascript - 将youtube/vimeo url转换为嵌入版本,用于论坛评论功能

在此输入图像描述

试图制作类似于facebook的东西.我已经创建了这个javascript url模式转换器.当用户点击论坛帖子的提交按钮时,可以触发这样的事情 - 将url转换为嵌入html变体.有什么方法可以改善吗?

http://jsfiddle.net/88Ms2/377/

var videoEmbed = {
    invoke: function(){

        $('body').html(function(i, html) {
            return videoEmbed.convertVideo(html);
        });

    },
    convertVideo: function(html){
        var pattern1 = /(?:http?s?:\/\/)?(?:www\.)?(?:vimeo\.com)\/?(.+)/g;
        var pattern2 = /(?:http?s?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g;

        if(pattern1.test(html)){
            console.log("html", html);

           var replacement = '<iframe width="420" height="345" src="//player.vimeo.com/video/$1" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';

           var html = html.replace(pattern1, replacement);
        }


        if(pattern2.test(html)){
              console.log("html", html);

           var replacement = '<iframe width="420" height="345" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>';

            var html = html.replace(pattern2, replacement);
        } 


        return html;
    }
}






setTimeout(function(){
    videoEmbed.invoke();
},3000);
Run Code Online (Sandbox Code Playgroud)

javascript jquery

5
推荐指数
1
解决办法
9957
查看次数

如何在粘贴时自动将URL转换为超链接

当我尝试在文本框中粘贴网址时,https://stackoverflow.com/它不会自动转换为超链接.

我尝试使用正则表达式这是我之前提出的问题.我在这个问题中使用的功能工作正常,但实际上它将替换所有链接,包括标签中的链接(IMG,现有的A HREF).

我不想使用regx,如果我使用regx转换发生时,我点击任何提交或保存按钮.

**当用户在文本框中粘贴网址时,它应自动将任何链接转换为超链接****

我用regx尝试过这个

例如:

what = "<span>In the task system, is there a way to automatically have any site / page URL or image URL be hyperlinked in a new window?</span><br><br><span>So If I type or copy http://www.stackoverflow.com/&nbsp; for example anywhere in the description, in any of the internal messages or messages to clients, it automatically is a hyperlink in a new window.</span><br><a href="http://www.stackoverflow.com/">http://www.stackoverflow.com/</a><br>    <br><span>Or if I input an image URL anywhere in …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery jquery-plugins

3
推荐指数
2
解决办法
2979
查看次数

标签 统计

javascript ×3

jquery ×2

html ×1

jquery-plugins ×1

string ×1

url ×1