带 url 的正则表达式

发布于 2024-12-18 13:15:44 字数 2092 浏览 3 评论 0原文

我检查给定的网址是否与另一个网址匹配(可以有通配符)。

例如,我有以下网址:

john.doe.de/foo

我现在想检查该网址是否有效,用户定义要检查的字符串,例如:

*.doe.de/*

效果很好,但使用以下内容它不应该工作,但会被接受:

*.doe.de

到目前为止我编写的函数,url 存储为 firefox prefs,并且“checkedLocationsArray”包含所有要检查的 url。

function checkURLS(index)
{
    if(index >= 0)
    {
    var pos = getPos("URL-Mask");
    var url = tables[index][pos];

    if(url != null && url != "")
    {
        var urlnow = "";
        if(redlist_pref.prefHasUserValue("table.1"))
        {
            var checkedLocationsArray = new Array();
            for(i = 0; i < tables.length; i++)
            {
                checkedLocationsArray[i] = tables[i][pos];
            }

            for(i=0;i<checkedLocationsArray.length;i++)
            {
                urlnow = checkedLocationsArray[i];

                if(urlnow == url)
                {
                    return true;
                }

                if(urlnow.indexOf('*.') != -1)
                {
                    while(urlnow.indexOf("*.") != -1)
                        urlnow = urlnow.replace("\*.", "\.[^\.]*");
                }
                if(urlnow.indexOf('.*') != -1)
                {
                    while(urlnow.indexOf(".*") != -1)
                        urlnow = urlnow.replace(".\*", "([^\.]*\.)");
                }
                if(urlnow.indexOf('/*') != -1)
                {
                    while(urlnow.indexOf("/*") != -1)
                        urlnow = urlnow.replace("/*", /\S\+*/)
                }
                else if(url.lastIndexOf('/') != -1)
                {
                    return false;
                }

                var regex = new RegExp(urlnow);
                var Erg = regex.exec(url);
                if(Erg != null)
                    return true;
            }
        }
        return false;
    }
}
}

我认为“else if(url.indexOf('/') != -1)”是重要的部分。它应该像这样工作得很好,如果我提醒它,我什至得到结果是真的,但似乎 if 没有被执行。 如果有任何不清楚的地方,请发表评论。提前致谢!

I check if a given url matches another one (which can have wildcards).

E.g. I have the following url:

john.doe.de/foo

I'd now like to check whether the url is valid or not, the user defines the string to check with, e.g:

*.doe.de/*

That works fine, but with the following it should NOT work but it gets accepted:

*.doe.de

Here the function i wrote till now, the urls are stored as firefox prefs and i the "checkedLocationsArray" containts all urls to be checked.

function checkURLS(index)
{
    if(index >= 0)
    {
    var pos = getPos("URL-Mask");
    var url = tables[index][pos];

    if(url != null && url != "")
    {
        var urlnow = "";
        if(redlist_pref.prefHasUserValue("table.1"))
        {
            var checkedLocationsArray = new Array();
            for(i = 0; i < tables.length; i++)
            {
                checkedLocationsArray[i] = tables[i][pos];
            }

            for(i=0;i<checkedLocationsArray.length;i++)
            {
                urlnow = checkedLocationsArray[i];

                if(urlnow == url)
                {
                    return true;
                }

                if(urlnow.indexOf('*.') != -1)
                {
                    while(urlnow.indexOf("*.") != -1)
                        urlnow = urlnow.replace("\*.", "\.[^\.]*");
                }
                if(urlnow.indexOf('.*') != -1)
                {
                    while(urlnow.indexOf(".*") != -1)
                        urlnow = urlnow.replace(".\*", "([^\.]*\.)");
                }
                if(urlnow.indexOf('/*') != -1)
                {
                    while(urlnow.indexOf("/*") != -1)
                        urlnow = urlnow.replace("/*", /\S\+*/)
                }
                else if(url.lastIndexOf('/') != -1)
                {
                    return false;
                }

                var regex = new RegExp(urlnow);
                var Erg = regex.exec(url);
                if(Erg != null)
                    return true;
            }
        }
        return false;
    }
}
}

i think the "else if(url.indexOf('/') != -1)" is the important part. It should work just fine like that, if I alert it I even get that the result is true but it seems like the if is not being executed..
If anything is unclear, please just post a comment. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生寂 2024-12-25 13:15:44

为什么不只添加字符串开头和结尾的字符?

function checkURLS(index)
{
    if(index >= 0)
    {
        var pos = getPos("URL-Mask");
        var url = tables[index][pos];

        if(url != null && url != "")
        {
            var urlnow = "";
            if(redlist_pref.prefHasUserValue("table.1"))
            {
                var checkedLocationsArray = new Array();
                for(i = 0; i < tables.length; i++)
                {
                    checkedLocationsArray[i] = tables[i][pos];
                }

                for(i=0;i<checkedLocationsArray.length;i++)
                {
                    urlnow = checkedLocationsArray[i];

                    if(urlnow == url)
                    {
                        return true;
                    }

                    //Check there's nothing else in the string
                    urlnow = '^' + urlnow + '
;

                    if(urlnow.indexOf('*') != -1)
                    {
                        while(urlnow.indexOf("*") != -1)
                            urlnow = urlnow.replace("\*", ".*");
                    }
                    else if(urlnow.lastIndexOf('/') != -1)
                    {
                        return false;
                    }

                    var regex = new RegExp(urlnow);
                    var Erg = regex.exec(url);
                    if(Erg != null)
                        return true;
                }
            }
            return false;
        }
    }
}

Why don't you just add the characters for beginning and end of string?

function checkURLS(index)
{
    if(index >= 0)
    {
        var pos = getPos("URL-Mask");
        var url = tables[index][pos];

        if(url != null && url != "")
        {
            var urlnow = "";
            if(redlist_pref.prefHasUserValue("table.1"))
            {
                var checkedLocationsArray = new Array();
                for(i = 0; i < tables.length; i++)
                {
                    checkedLocationsArray[i] = tables[i][pos];
                }

                for(i=0;i<checkedLocationsArray.length;i++)
                {
                    urlnow = checkedLocationsArray[i];

                    if(urlnow == url)
                    {
                        return true;
                    }

                    //Check there's nothing else in the string
                    urlnow = '^' + urlnow + '
;

                    if(urlnow.indexOf('*') != -1)
                    {
                        while(urlnow.indexOf("*") != -1)
                            urlnow = urlnow.replace("\*", ".*");
                    }
                    else if(urlnow.lastIndexOf('/') != -1)
                    {
                        return false;
                    }

                    var regex = new RegExp(urlnow);
                    var Erg = regex.exec(url);
                    if(Erg != null)
                        return true;
                }
            }
            return false;
        }
    }
}
蓝海似她心 2024-12-25 13:15:44

问题似乎是您没有检查字符串的开头和结尾。将代码更改为类似

urlnow = '^'+urlnow+'

^ 是字符串开始的正则表达式代码,$ 是字符串结束的代码。这样您就可以确保整个 url 必须与该模式匹配,而不仅仅是其中的一部分。

; // this is new var regex = new RegExp(urlnow);

^ 是字符串开始的正则表达式代码,$ 是字符串结束的代码。这样您就可以确保整个 url 必须与该模式匹配,而不仅仅是其中的一部分。

The problem seems that you don't check for the start and the end of the string. Change your code to something like

urlnow = '^'+urlnow+'

^ is the RegExp code for string-start and $ the code for string-end. That way you ensure that the whole url has to match the pattern and not only a part of it.

; // this is new var regex = new RegExp(urlnow);

^ is the RegExp code for string-start and $ the code for string-end. That way you ensure that the whole url has to match the pattern and not only a part of it.

提赋 2024-12-25 13:15:44

我发现我做的网址不是当前的网址,我在下面更改了它。

我还更改了 * 现在被替换为正则表达式,并且在这种情况下点必须存在。

function redlist_checkURLS(index)
{
if(index >= 0)
{
    var pos = getPos("URL-Mask");
    var url = currenturl.replace("http://", "");

    if(url != null && url != "")
    {
        var urlnow = "";
        if(pref.prefHasUserValue("table.1"))
        {
            var urlsok = 0;
            var checkedLocationsArray = new Array();
            for(i = 0; i < tables.length; i++)
            {
                checkedLocationsArray[i] = tables[i][pos];
            }

            for(i=0;i<checkedLocationsArray.length;i++)
            {
                urlnow = checkedLocationsArray[i];

                if(urlnow == url)
                {
                    return true;
                }

                if(urlnow.indexOf('*') != -1)
                {
                    while(urlnow.indexOf("*") != -1)
                        urlnow = urlnow.replace("*", "\\S+")
                }

                var regex = new RegExp(urlnow);
                var Erg = regex.exec(url);

                if(Erg != null && Erg == url)
                    return true;
            }
        }
        return false;
    }
}
}

感谢您的帮助思考!

I figured out that I did the url was not the current url, I changed that below.

I also changed that the * are now replaced with a regular expression and the dots have to be there in this situation.

function redlist_checkURLS(index)
{
if(index >= 0)
{
    var pos = getPos("URL-Mask");
    var url = currenturl.replace("http://", "");

    if(url != null && url != "")
    {
        var urlnow = "";
        if(pref.prefHasUserValue("table.1"))
        {
            var urlsok = 0;
            var checkedLocationsArray = new Array();
            for(i = 0; i < tables.length; i++)
            {
                checkedLocationsArray[i] = tables[i][pos];
            }

            for(i=0;i<checkedLocationsArray.length;i++)
            {
                urlnow = checkedLocationsArray[i];

                if(urlnow == url)
                {
                    return true;
                }

                if(urlnow.indexOf('*') != -1)
                {
                    while(urlnow.indexOf("*") != -1)
                        urlnow = urlnow.replace("*", "\\S+")
                }

                var regex = new RegExp(urlnow);
                var Erg = regex.exec(url);

                if(Erg != null && Erg == url)
                    return true;
            }
        }
        return false;
    }
}
}

Thanks for your help thought!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文