如何仅使用正则表达式验证YouTube嵌入式链接 [英] How to validate the YouTube embedded link only using regular expression

查看:68
本文介绍了如何仅使用正则表达式验证YouTube嵌入式链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在网站上使用YouTube嵌入式链接.我想验证该链接,就像用户粘贴嵌入式链接以外的其他内容一样,它应该给我一个警告无效的URL.我已经使用了很多正则表达式,一些已经在我的代码中发表了评论.我只想对YouTube嵌入式链接进行正则表达式.这是我的代码:

I am using the YouTube embedded link in my website .I want to validate the link as if user paste something else other then embedded link then it should give me an alert invalid URL. I have used so many regex some has already in my code i have commented it .I want regular expression of YouTube embedded link only. Here i my code:

package com.edubot.client.lecture;

import gwt.material.design.client.ui.MaterialButton;
import gwt.material.design.client.ui.MaterialTextBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;

public class EmbeddedLink extends Composite  {

    private static EmbeddedLinkUiBinder uiBinder = GWT
            .create(EmbeddedLinkUiBinder.class);

    interface EmbeddedLinkUiBinder extends UiBinder<Widget, EmbeddedLink> {
    }

    @UiField MaterialButton buttonembedded;
    //  @UiField IFrameElement youtubevideo;
    @UiField HTMLPanel htmlpanel;
    @UiField MaterialTextBox textbox ;



    public EmbeddedLink() {
        super();
        sinkEvents( Event.ONPASTE );
        initWidget(uiBinder.createAndBindUi(this));

    }

    @Override
    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);
        switch (event.getTypeInt()) {
        case Event.ONPASTE: {
            Timer timer  = new Timer() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    onPasted();
                    Window.alert("paste");
                }
            };
            timer.schedule(1000);
        }
        }

    }

    //  @UiHandler("buttonembedded")
    //      void onClick(ClickEvent e) { 
    ////        onPasted(); 
    //      }


    private void onPasted(){
        //      youtubevideo.setSrc(addEmbeddedLink());
        Window.alert("msg1");
        if(testEmbeddedLink()) {
            String link=textbox.getText().trim();
            htmlpanel.getElement().setInnerHTML(link);
            Window.alert("Valid URL");
        } else {
            Window.alert("Invalid URL");
        }


    }


    public boolean testEmbeddedLink(){
        String link=textbox.getText().trim();
        Window.alert("msg");
        String patternString = "(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/)(?:)(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/[a-zA-Z0-9\-]*";
        //      String patternString = "<iframe title='YouTube video player' width='' height='' src='http://www.youtube.com/embed/$1' frameborder='0' allowfullscreen='1'></iframe>";
        //      String patternString = "~<iframe.+?src="https?://www.youtube.com/embed/([a-zA-Z0-9_-]{11})"[^>]+?></iframe>~i";
        boolean result = link.matches(patternString);

        return result;

    }
    //  "youtube.com/(?<=v|embed\\)?[a-zA-Z0-9]+[^#\\&\\?]*";
    //  "(?<=youtu.be/?<=v|embed\\/)?[a-zA-Z0-9]+[^#\\&\\?]*";
    //  "(https?://)?(www\\.)?(yotu\\.be/|youtube\\.com/)?((.+/)?(watch(\\?v=|.+&v=))?(v=)?)([\\w_-]{11})(&.+)?"
    //  (\"http:\/\/www\.youtube\.com\/v\/\w{11}\&rel\=1\");
    //  (https?://www.youtube(?:-nocookie)?.com/(?:v|embed)/([a-zA-Z0-9-]+).*)"
    //  /<iframe.+?src="http://www.youtube.com/embed/([a-zA-Z0-9_-]{11})"[^>]+?>";</iframe>/i";
    //   "(?:youtube.com)\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\W";
    //  "s*(https?://www.youtube(?:-nocookie)?.com/(?:v|embed)/([a-zA-Z0-9-]+).*) ";
//   "^.*((youtu.be"+ "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*)

}

推荐答案

private String getYouTubeUrl(String text)
    {
        String finalUrl = null;
        String p = "(//www.youtube(?:-nocookie)?.com/(?:v|embed)/([a-zA-Z0-9-_]+).*)";

        if(text.contains("src"))
        {
            if(text.contains("//") && text.contains("frameborder"))
            {
                int startpos = text.indexOf("/", text.indexOf("src="));
                int endpos = text.indexOf("frameborder");
                String url=text.substring(startpos, endpos-2);

                if(url.matches(p))
                {
                    finalUrl = url;
                }
                else
                {
                    Window.alert("You have entered a wrong embed code");
                }   
            }
            else
            {
                Window.alert("You have entered a wrong embed code");
            }   
        }
        else
        {
            Window.alert("You have entered a wrong embed code");
        }
        return finalUrl;
    }

这篇关于如何仅使用正则表达式验证YouTube嵌入式链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆