I am trying to get Video ID from entered youtube url with regular expression.
Below is the Regular expression I am using :
/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^#\&\?]*).*/
But It's coming back as
Uncaught SyntaxError: Invalid regular expression:
//^.*(youtu.be/|v/|u/w/|embed/|watch?v=|&v=|?v=)([^#&?]*).*//
: Nothing to repeat
Why?
Below is my code:
function PreviewVideo() {
var self = this;
this.links = $(".videoPreview");
this.bindEvents = function () {
self.links.on("click", self.fireModal);
};
this.getYoutubeId = function (url) {
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[2].length == 11) {
return match[2];
} else {
return false;
}
};
this.fireModal = function (e) {
e.preventDefault;
var $this = $(this);
var videoField = $this.data("video");
var videoURL = $(videoField).val();
if (videoURL === "") {
$("body").append(UI.toast({level: "danger", time: 5000, html: "<h4>No video URL found</h4>", dismissable: true, position: "top-right"}));
} else {
var videoID = self.getYoutubeId(videoURL);
if (videoID) {
UI.modal({title: "Video preview", body: '<iframe width="560" height="315" src="//www.youtube.com/embed/' + videoID + '" frameborder="0" allowfullscreen></iframe>'})
} else {
$("body").append(UI.toast({level: "danger", time: 5000, html: "<h4>Invalid URL</h4>", dismissable: true, position: "top-right"}));
}
}
};
this.bindEvents();
}