Javascript 从facebook url提取id值
我想要一个JavaScript正则表达式从facebook个人资料URL中提取id的值。例如,我有一个urlJavascript 从facebook url提取id值,javascript,regex,Javascript,Regex,我想要一个JavaScript正则表达式从facebook个人资料URL中提取id的值。例如,我有一个url https://www.facebook.com/profile.php?id=100004774025067 我只想提取id=后面的值,这是数字10000474025067部分 但在某些情况下,我需要一篇文章中的用户配置文件url。例如,一个用户发布了一些内容,如果我得到了该帖子的profile url链接,那么我得到的链接如下: https://www.facebook.com/
https://www.facebook.com/profile.php?id=100004774025067
我只想提取id=
后面的值,这是数字10000474025067
部分
但在某些情况下,我需要一篇文章中的用户配置文件url。例如,一个用户发布了一些内容,如果我得到了该帖子的profile url链接,那么我得到的链接如下:
https://www.facebook.com/profile.php?id=100001883994837&hc_ref=ART7eWRecFS8mMIio66GdaH378zlJMXzisnKubh5PtgINeVwTfOil5aBIyff71OamWA
如您所见,在id
的值之后,指定了一个附加参数
我只需要获取
id
值,无论链接中还有其他内容。您可以将字符串用作URL,并使用searchParams
提取id
参数:
无附加参数:
var fb_url=”https://www.facebook.com/profile.php?id=100004774025067";
var url=新的url(fb_url);
var uid=url.searchParams.get(“id”);
控制台日志(uid)
您可以使用字符串作为URL,并使用searchParams
提取id
参数:
无附加参数:
var fb_url=”https://www.facebook.com/profile.php?id=100004774025067";
var url=新的url(fb_url);
var uid=url.searchParams.get(“id”);
控制台日志(uid)代码>您可以按以下方式执行
函数getNumber(str){
log(str.match(/(?:id=)(\d+/)[1]);
返回str.match(/(?:id=)(\d+/)[1];
}
getNumber('https://www.facebook.com/profile.php?id=100004774025067');
getNumber('https://www.facebook.com/profile.php?id=100001883994837&hc_ref=ART7eWRecFS8mMIio66GdaH378zlJMXzisnKubh5PtgINeVwTfOil5aBIyff71OamWA ');代码>您可以按以下方式执行
函数getNumber(str){
log(str.match(/(?:id=)(\d+/)[1]);
返回str.match(/(?:id=)(\d+/)[1];
}
getNumber('https://www.facebook.com/profile.php?id=100004774025067');
getNumber('https://www.facebook.com/profile.php?id=100001883994837&hc_ref=ART7eWRecFS8mMIio66GdaH378zlJMXzisnKubh5PtgINeVwTfOil5aBIyff71OamWA ');代码>谢谢,这是一个完美的解决方案谢谢,这是一个完美的解决方案