我正在尝试将此python正则表达式转换为javascript正则表达式
r"""(?x)^
(
(?:https?://|//)? # http(s):// or protocol-independent URL (optional)
(?:(?:(?:(?:\w+\.)?[yY][oO][uU][tT][uU][bB][eE](?:-nocookie)?\.com/|
(?:www\.)?deturl\.com/www\.youtube\.com/|
(?:www\.)?pwnyoutube\.com/|
(?:www\.)?yourepeat\.com/|
tube\.majestyc\.net/|
youtube\.googleapis\.com/) # the various hostnames, with wildcard subdomains
(?:.*?\#/)? # handle anchor (#/) redirect urls
(?: # the various things that can precede the ID:
(?:(?:v|embed|e)/) # v/ or embed/ or e/
|(?: # or the v= param in all its forms
(?:(?:watch|movie)(?:_popup)?(?:\.php)?/?)? # preceding watch(_popup|.php) or nothing (like /?v=xxxx)
(?:\?|\#!?) # the params delimiter ? or # or #!
(?:.*?&)? # any other preceding param (like /?s=tuff&v=xxxx)
v=
)
))
|youtu\.be/ # just youtu.be/xxxx
|https?://(?:www\.)?cleanvideosearch\.com/media/action/yt/watch\?videoId=
)
)? # all until now is optional -> you can pass the naked ID
([0-9A-Za-z_-]{11}) # here is it! the YouTube video ID
(?(1).+)? # if we found the ID, everything can follow
$"""
我在开始和结束时删除了引号,添加了开始/^
和结束分隔符/i
,转义了正斜杠,删除了自由间距模式并最终使用了此
var VALID_URL = /^((?:https?:\/\/|\/\/)?(?:(?:(?:(?:\w+\.)?[yY][oO][uU][tT][uU][bB][eE](?:-nocookie)?\.com\/|(?:www\.)?deturl\.com\/www\.youtube\.com\/|(?:www\.)?pwnyoutube\.com\/|(?:www\.)?yourepeat\.com\/|tube\.majestyc\.net\/|youtube\.googleapis\.com\/)(?:.*?\#\/)?(?:(?:(?:v|embed|e)\/)|(?:(?:(?:watch|movie)(?:_popup)?(?:\.php)?\/?)?(?:\?|\#!?)(?:.*?&)?v=)))|youtu\.be\/|https?:\/\/(?:www\.)?cleanvideosearch\.com\/media\/action\/yt\/watch\?videoId=))?([0-9A-Za-z_-]{11})(?(1).+)?$/g;
然而,我正在使用的javascript正则表达式调试器{1转义此部分python正则表达式的javascript转置
Unexpected character "(" after "?"
知道如何解决此错误吗?
答案 0 :(得分:1)
JavaScript不支持条件。
但是,正则表达的世界早已没有条件存在,并且有很多方法可以解决它。理念
这个可怕的正则表达式的基本结构是:
(Capture A)? (Match B) ( If A was captured, (Match C)? )
您可以将 IF 翻译为 OR :
(Capture A) (Match B) (Match C)? **OR** (Match B)
已转换的正则表达式
试试这个:
^((?:https?://|//)?(?:(?:(?:(?:\w+\.)?[yY][oO][uU][tT][uU][bB][eE](?:-nocookie)?\.com/|(?:www\.)?deturl\.com/www\.youtube\.com/|(?:www\.)?pwnyoutube\.com/|(?:www\.)?yourepeat\.com/|tube\.majestyc\.net/|youtube\.googleapis\.com/)(?:[^\n]*?#/)?(?:(?:(?:v|embed|e)/)|(?:(?:(?:watch|movie)(?:_popup)?(?:\.php)?/?)?(?:\?|#!?)(?:[^\n]*?&)?v=)))|youtu\.be/|https?://(?:www\.)?cleanvideosearch\.com/media/action/yt/watch\?videoId=)([0-9A-Za-z_-]{11})(?:[^\n]+)?)|^([0-9A-Za-z_-]{11})
<强>解释强>
如果设置了组1,则(?(1)[^\n]+)?
条件尝试可选地匹配[^\n]+
。由于它发生在非可选([0-9A-Za-z_-]{11})
之后,我将条件转换为交替|
([0-9A-Za-z_-]{11})
和可选组件 OR ([0-9A-Za-z_-]{11})
([0-9A-Za-z_-]{11})
,根据轮换的哪一侧与之匹配,它将位于不同的捕获组内。我将让你算上括号。<强>参考强>