我知道这听起来像是一个常见问题,但是我在这里有不同的变量:
我有一个像这样的网址:

https://www.facebook.com/events/546604752058417/?suggestsessionid=791e61ca005570613fa552635bc794a5


现在,我需要获取数字546604752058417。因此,从逻辑上讲,我应该得到events/之后的所有内容,但要得到546604752058417之后的第一个斜杠之后的所有内容

现在的问题是,网址是否可以以http,https,www等开头。

我有点迷茫,对Java语言还是陌生的。

简单的方法是什么?

我有一个检查Url是否有效且来自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 - 从网址获取特定的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17105828/

10-09 18:14
查看更多