Extract parameter value from url using regular expressions

后端 未结 6 1667
抹茶落季
抹茶落季 2020-12-08 06:19

This should be very simple (when you know the answer). From this question

I want to give the posted solution a try. My question is:

How to get the parameter

6条回答
  •  情书的邮戳
    2020-12-08 07:10

    You almost had it, just need to escape special regex chars:

    regex = /http\:\/\/www\.youtube\.com\/watch\?v=([\w-]{11})/;
    
    url = 'http://www.youtube.com/watch?v=Ahg6qcgoay4';
    id = url.match(regex)[1]; // id = 'Ahg6qcgoay4'
    

    Edit: Fix for regex by soupagain.

提交回复
热议问题