function glob2regexp(glob) {
const prefix = "^/?(.*/)*";
const suffix = "$";
const content = glob
.replace(/\./g, ".")
.replace(/\*{2,}/, "**")
.replace(/\*{1,}\/?/g, function (match, b, c, d) {
const matchSlash = match[match.length - 1] === "/";
match = !matchSlash ? match : match.substr(0, match.length - 1);
if (match === "**") {
return matchSlash ? "((.*)/)*" : "(.*)";
} else {
return matchSlash ? "(.+)" + "/" : "(.+)";
}
});
const globRegExpString = prefix + content + suffix;
const reg = new RegExp(globRegExpString);
return reg;
}
console.log(glob2regexp("views-*/**/*.js").test("views-admin/hello/123123/test.js"));
// true
console.log(glob2regexp("views-*/**/*.js").test("views-admin/test.js"));
// true
大牛们的评论:朕有话说
还没有人评论哦,赶紧抢沙发!