我正在使用我网站上的YouTube嵌入式链接。我想验证链接,就好像用户粘贴了嵌入式链接以外的其他内容,然后它应该给我一个提醒无效的网址。我已经使用了很多正则表达式,我已经在我的代码中评论了它。我只想要正常表达YouTube嵌入式链接。这是我的代码:
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?=?([^#\\&\\?]*)
}
答案 0 :(得分: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;
}