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