Skip to content

Commit fa81e77

Browse files
committed
🐛 支持一些正则表达式
1 parent ccd4085 commit fa81e77

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/pkg/utils/match.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("UrlMatch-google-error", () => {
4646
});
4747

4848
// 从tm找的一些特殊的匹配规则
49-
describe("UrlMatch-search", () => {
49+
describe("UrlMatch-special", () => {
5050
const url = new UrlMatch<string>();
5151
url.add("https://www.google.com/search?q=*", "ok1");
5252
it("match1", () => {
@@ -88,6 +88,14 @@ describe("UrlMatch-search", () => {
8888
expect(url.match("http://www.example.com")).toEqual(["ok1"]);
8989
expect(url.match("https://www.example.com")).toEqual(["ok1"]);
9090
});
91+
it("/^.*?://.*?.example.com.*?$/", () => {
92+
const url = new UrlMatch<string>();
93+
url.add("/^.*?://.*?.example.com.*?$/", "ok1");
94+
expect(url.match("https://www.example.com")).toEqual(["ok1"]);
95+
expect(url.match("http://www.example.com")).toEqual(["ok1"]);
96+
expect(url.match("https://api.example.com/foo/bar")).toEqual(["ok1"]);
97+
expect(url.match("https://api.foo.example.com/foo/bar")).toEqual(["ok1"]);
98+
});
9199
});
92100

93101
describe("UrlMatch-port1", () => {
@@ -148,6 +156,7 @@ describe("dealPatternMatches", () => {
148156
"*://*example.com/*",
149157
"*.example.com/path/*",
150158
"http*",
159+
"/^.*?://.*?.example.com.*?$/",
151160
]);
152161
expect(matches.patternResult).toEqual([
153162
"*://*/*",
@@ -156,6 +165,7 @@ describe("dealPatternMatches", () => {
156165
"*://example.com/*",
157166
"*://*.example.com/path/*",
158167
"*://*/*",
168+
"*://*/*",
159169
]);
160170
expect(matches.result).toEqual([
161171
"*://www.example.com*",
@@ -164,6 +174,7 @@ describe("dealPatternMatches", () => {
164174
"*://*example.com/*",
165175
"*.example.com/path/*",
166176
"http*",
177+
"/^.*?://.*?.example.com.*?$/",
167178
]);
168179
});
169180
it("特殊情况-exclude", () => {

src/pkg/utils/match.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,19 @@ export default class Match<T> {
122122
public add(url: string, val: T) {
123123
// 判断是不是一个正则
124124
if (url.startsWith("/^") || url.endsWith("$/")) {
125+
// 删除开头和结尾的/
126+
if (url.startsWith("/")) {
127+
url = url.substring(1);
128+
}
129+
if (url.endsWith("/")) {
130+
url = url.substring(0, url.length - 1);
131+
}
125132
this.addRegex(url, val);
133+
return;
126134
}
127135
const re = this.compileRe(url);
128136
if (!re) {
129-
console.warn("bad match rule", { url, val });
137+
console.warn("add failed: bad rule", { url, val });
130138
return;
131139
}
132140
this.addRegex(re, val);
@@ -145,7 +153,7 @@ export default class Match<T> {
145153
ret.push(...val);
146154
}
147155
} catch (e) {
148-
console.warn("bad match rule", { val }, Logger.E(e));
156+
console.warn("match failed: bad rule", { val }, Logger.E(e));
149157
// LoggerCore.getLogger({ component: "match" }).warn(
150158
// "bad match rule",
151159
// Logger.E(e)

0 commit comments

Comments
 (0)