【问题标题】:Wildcard in javascript pathjavascript路径中的通配符
【发布时间】:2011-06-14 22:03:42
【问题描述】:

我需要在 if 语句的末尾指定一个带有通配符的路径.. 就像文件系统中的 * 一样

if(document.location.pathname == "/project/*") { jQuery("#regbutton").css('display','none'); };

ie.. if 应该为所有以 /project/ 开头的路径触发

在不使用狂野的正则表达式的情况下,有什么方法可以做到这一点?
jquery 解决方案也会很棒..

thnx

【问题讨论】:

    标签: javascript jquery path wildcard


    【解决方案1】:

    这使用了一个正则表达式,但它和正则表达式差不多。

    if(document.location.pathname.match(/^\/project\//))
    {
        jQuery("#regbutton").css('display','none');
    }
    

    【讨论】:

    【解决方案2】:

    你可以这样做:-

    String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
    
    if(document.location.pathname.startsWith("/project/")) {
       ...
    } 
    

    这样,下次你可以随时继续使用startsWith()函数。

    【讨论】:

    • 还没有尝试过,但我会......我喜欢可重复使用的想法,谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多