无效的正则表达式——无需重复——Javascript

时间:2021-08-07 20:25:30

I am trying to get Video ID from entered youtube url with regular expression.

我正在尝试从输入的youtube url获取视频ID,并使用正则表达式。

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

未捕获SyntaxError:无效的正则表达式:/ / ^。*(youtu.be / | v / u / w / | |嵌入/ |手表吗? v = |增加了= | ? v =)((^ # & ?)*)。* / /:没有重复

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();

}

}

1 个解决方案

#1


1  

As you can see from the error message, the regular expression that is somehow produced by your code is not valid:

从错误消息中可以看出,代码以某种方式生成的正则表达式是无效的:

//^.*(youtu.be/|v/|u/w/|embed/|watch?v=|&v=|?v=)([^#&?]*).*//

The reason for this is that you put the whole regex literal inside a string:

原因是您将整个regex文字放在一个字符串中:

var regExp = "/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^‌​#\&\?]*).*/";

This cannot work, partly because \ is also the escape charter in string literals, so when you write &v=|\?v=, what is passed to the regex engine is &v=|?v= (and |? is not valid).

这是行不通的,部分原因是,在字符串文本中,\也是转义包,所以当您写入&v=|\?v=,传递给regex引擎的是&v=|?v = | ?是无效的)。

Simply use the regex literal:

简单地使用regex文字:

var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^‌​#\&\?]*).*/;

#1


1  

As you can see from the error message, the regular expression that is somehow produced by your code is not valid:

从错误消息中可以看出,代码以某种方式生成的正则表达式是无效的:

//^.*(youtu.be/|v/|u/w/|embed/|watch?v=|&v=|?v=)([^#&?]*).*//

The reason for this is that you put the whole regex literal inside a string:

原因是您将整个regex文字放在一个字符串中:

var regExp = "/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^‌​#\&\?]*).*/";

This cannot work, partly because \ is also the escape charter in string literals, so when you write &v=|\?v=, what is passed to the regex engine is &v=|?v= (and |? is not valid).

这是行不通的,部分原因是,在字符串文本中,\也是转义包,所以当您写入&v=|\?v=,传递给regex引擎的是&v=|?v = | ?是无效的)。

Simply use the regex literal:

简单地使用regex文字:

var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^‌​#\&\?]*).*/;