使用正则表达式从url中提取参数值 [英] Extract parameter value from url using regular expressions
本文介绍了使用正则表达式从url中提取参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这应该非常简单(当您知道答案时).来自这个问题
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:
如何使用JavaScript正则表达式获取给定URL的参数值?
How to get the parameter value of a given URL using JavaScript regular expressions?
我有:
http://www.youtube.com/watch?v=Ahg6qcgoay4
我需要:
Ahg6qcgoay4
我尝试过:
http://www.youtube.com/watch\\?v=(w{11})
但是:我很烂...
推荐答案
您几乎拥有它,只需要转义特殊的正则表达式字符即可:
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'
通过 查看全文