Javascript regular expression for extracting Youtube video ids

后端 未结 4 1255
清酒与你
清酒与你 2021-01-15 05:35

The following code is used to get Youtube video ids in order to get a thumbnail image.

What is the reasoning behind the first regular expression and what is it doing

4条回答
  •  梦毁少年i
    2021-01-15 06:14

    "[\\?&]v=([^&#]*)"
    

    explained (after reduction from a JavaScript string to a regex):

    [\?&]   # Match a ? or a & (the backslash is unnecessary here!)
    v=      # Match the literal text "v="
    (       # Capture the following into backreference no. 1:
     [^&#]* # Zero or more characters except & or #
    )       # End of capturing group.
    

    The second regex [^http://youtu.be/](.*)[^?hd=1] is very wrong.

    It probably should read

    "^http://youtu.be/(.*)(?=hd=1)"
    

提交回复
热议问题