首页 > 其他 > 详细

路径的正则通配符-常用

时间:2016-11-08 01:47:04      阅读:141      评论:0      收藏:0      [点我收藏+]
function regDir(str){
    var reg=str
    if(typeof reg=="string"){
        reg=reg.replace(/[\[\]\\\^\:\.\?\+]/g,function(m){
            return "\\"+m;
        })
        reg=reg.replace(/\*\*/g,function(m){
            return ".+";
        })
        reg=reg.replace(/\*/g,function(m){
            return "[\\w#\\.-]+";
        })
        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/a.js?name"
console.log(str.Test("http://(**)(?*)"))

//路径替换
var b="http://www.baidu.com/a.js?sdf#21"
var reg=regDir("http://(**)?*")
console.log(b.replace(reg,"$1"))

  输出

true
www.baidu.com/a.js

路径的正则通配符-常用

原文:http://www.cnblogs.com/caoke/p/6041126.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!