How to validate the YouTube embedded link only using regular expression

懵懂的女人 提交于 2021-02-07 04:17:51

问题


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?=?([^#\\&\\?]*)

}

回答1:


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;
    }


来源:https://stackoverflow.com/questions/44041062/how-to-validate-the-youtube-embedded-link-only-using-regular-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!