-1

这是一个从 youtube url 中提取视频 id 的函数。

 function youtubeLinkParser(url) {
            var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
            var match = url.match(regExp);
            if (match && match[2].length == 11) {
                return match[2];
            } else {
                return null;
            }
        }

我是正则表达式的新手,所以有人会介意将正则表达式分解成更小的部分并解释它是如何工作的。

4

3 回答 3

2

这是来自 Yape::Regex::Explain 的解释

The regular expression:

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

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    youtu                    'youtu'
----------------------------------------------------------------------
    .                        any character except \n
----------------------------------------------------------------------
    be/                      'be/'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    v/                       'v/'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    u/                       'u/'
----------------------------------------------------------------------
    \w                       word characters (a-z, A-Z, 0-9, _)
----------------------------------------------------------------------
    /                        '/'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    embed/                   'embed/'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    watch                    'watch'
----------------------------------------------------------------------
    \?                       '?'
----------------------------------------------------------------------
    v=                       'v='
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \&                       '&'
----------------------------------------------------------------------
    v=                       'v='
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [^#\&\?]*                any character except: '#', '\&', '\?' (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-06-13T08:19:10.043 回答
0
^.*

一开始,什么都有。

然后是这些事情之一:

youtu.be/    (that's the intention, but actually the dot can be any char)
v/
u/some letter/
embed/
watch?v=
&v=

上面的东西变成了match[1]。

然后是零个或多个不是 #& 或 ? 这些字符变为 match[2]。

最终什么都会到来。

于 2013-06-13T08:19:56.530 回答
0

/^.*以任何字符开头

(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)

匹配以下任何一项:

  • youtu*be/ <-- 应该是 youtu.be
  • v/
  • 你/w/
  • 看?v=
  • &v=

    ([^#\&\?]*)

后跟除 # & 和 ? 象征

.*/

任何字符直到最后

于 2013-06-13T08:23:56.023 回答