开发者

Problem with Javascript regex

开发者 https://www.devze.com 2023-03-26 20:52 出处:网络
function isOembed(url) { var pattern = /http:\\/\\/(.*youtube\\.com\\/watch.*|.*\\.youtube\\.com\\/v\\/.*|youtu\\.be\\/.*|.*\\.youtube\\.com\\/user\\/.*|.*\\.youtube\\.com\\/.*#.*\\/.*|m\\.youtube\\.c
function isOembed(url) {
    var pattern = /http:\/\/(.*youtube\.com\/watch.*|.*\.youtube\.com\/v\/.*|youtu\.be\/.*|.*\.youtube\.com\/user\/.*|.*\.youtube\.com\/.*#.*\/.*|m\.youtube\.com\/watch.*|m\.youtube\.com\/index.*|.*\.youtube\.com\/profile.*/;
    return pattern.test(url);
}

alert(isOembed('http://www.youtube.com/watch?v=9W8Ie4MyRX0&feature=related'));

Why does the following regex produce nothing? I've tried it in jsFiddle.

The lint says:

Error:
Problem at line 2 character 223: Unescaped '/'.
v开发者_Python百科ar pattern =/http:\/\/(.*youtube\.com\/watch.*|.*\.youtube\.com\/v\/.*|y...

Implied global: alert 6


jonnnnnnnnnie was right, just add a ) at end of the regex, like this: http://jsbin.com/ufufed/2/

var pattern = /http:\/\/(.*youtube\.com\/watch.*|.*\.youtube\.com\/v\/.*|youtu\.be\/.*|.*\.youtube\.com\/user\/.*|.*\.youtube\.com\/.*#.*\/.*|m\.youtube\.com\/watch.*|m\.youtube\.com\/index.*|.*\.youtube\.com\/profile.*)/;
0

精彩评论

暂无评论...
验证码 换一张
取 消