路径path的正则通配符-nodejs

function regDir(str){
    var reg=str
    if(typeof reg=="string"){
        reg=reg.replace(/[\[\]\\\^\:\.\?\+]/g,function(m){
            return "\\"+m;
        })
        reg=reg.replace(/\*\*|\*/g,function(m){
            if(m=="**"){
                return "[\\w\\W]*";
            }else{
                return "[^\\\/]*";
            }

        })
        reg=new RegExp(reg,"gi")
    }
    return reg
}
String.prototype.Test=function(regStr){
    var reg=regDir(regStr)
    return reg.test(this)
}
String.prototype.Replace=function(regStr,fn){
    var reg=regDir(regStr)
    return this.replace(reg,fn);
}
//是否符合
var str="http://www.baidu.com/b/da.js?n=21"
str=str.Replace("(http://**/*)?*",function(m,p1,p2){
    console.log(p1)
    return p1
})
str.Replace("http:/(/**/)*",function(m,p1,p2){
    console.log(p1)
})

  

posted @ 2016-11-08 13:17  巅峰蜗牛  阅读(5640)  评论(0编辑  收藏  举报