我知道这听起来可能是一个常见问题,但我这里有不同的变量: 我有一个像这样的网址:
https://www.facebook.com/events/546604752058417/?suggestsessionid=791e61ca005570613fa552635bc794a5
现在我需要获取号码546604752058417
。所以从逻辑上讲,我应该得到 events/
之后的所有内容,但 546604752058417
现在的问题是,Url 可能以 http、https、www 等开头,也可能不以 http、https、www 等开头。
我对 Javascript 有点迷茫和陌生。
有什么简单的方法吗?
我有一个函数可以检查网址是否有效并且来自 Facebook,但我现在不知道如何获取该网址546604752058417
。
显然这只是一个示例。该函数应该能够与任何事件 ID 一起使用。
function fbProcessSearch() {
var search_url = $("#search_fb_url").val();
if(isFbUrl(search_url)) {
$("#search_fb_event").validationEngine("hide");
// gets the event id from the Url and passes it to the below function.
fbProcess(eid);
}else{
$("#search_fb_event").validationEngine("showPrompt", "Please enter the Facebook event Url", "load", "topLeft", true);
}
}
function isFbUrl(url) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(url) && url.indexOf("facebook.com") > -1;
}
最佳答案
将 .match
与此正则表达式结合使用:/events\/(.*?)\//
"https://www.facebook.com/events/546604752058417/?suggestsessionid=791e61ca005570613fa552635bc794a5".match(/events\/(.*?)\//)[1]
=> "546604752058417"
说明:
events\/ -> Match "/events/" literally
(.*?)\/ -> match anything until first "/" and also capture it
关于javascript - 从Url中获取特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17105828/