Skip to content

Commit cf67857

Browse files
committed
refactor: use replaceAll instead of g mark
1 parent 35d28f0 commit cf67857

3 files changed

Lines changed: 6 additions & 10 deletions

File tree

src/pages/background/request-handler/web-request-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class WebRequestHandler {
261261
}
262262
} else if (item.to) {
263263
if (item.condition?.regex || item.matchType === 'regexp') {
264-
const to = redirectTo.replace(item._reg, item.to);
264+
const to = redirectTo.replaceAll(item._reg, item.to);
265265
logger.debug(
266266
`[web-request-handler] [rule: ${item.id}] redirect ${redirectTo} to ${to}`,
267267
);

src/pages/options/sections/rules/edit/form-content/test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ const Test = () => {
8282
if (detectRunner(initdRule) === 'dnr' && initdRule._re2 && ENABLE_DNR) {
8383
redirect = initdRule._re2.matcher(url).replaceAll(initdRule.to);
8484
} else if (initdRule?._reg) {
85-
initdRule._reg.lastIndex = 0;
86-
redirect = url.replace(initdRule._reg, initdRule.to);
85+
redirect = url.replaceAll(initdRule._reg, initdRule.to);
8786
} else {
8887
redirect = initdRule.to;
8988
}

src/share/core/rule-utils.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ export function initRule(
5252
if (rule.condition) {
5353
const { regex, excludeRegex } = rule.condition;
5454
if (regex) {
55-
initd._reg = new RegExp(regex, 'g');
55+
initd._reg = new RegExp(regex);
5656
}
5757
if (excludeRegex) {
5858
initd._exclude = new RegExp(excludeRegex);
5959
}
6060
} else {
6161
if (initd.matchType === 'regexp' && initd.pattern) {
62-
initd._reg = new RegExp(initd.pattern, 'g');
62+
initd._reg = new RegExp(initd.pattern);
6363
}
6464
if (typeof initd.exclude === 'string' && initd.exclude.length > 0) {
6565
initd._exclude = new RegExp(initd.exclude);
@@ -212,8 +212,7 @@ export function isMatchUrl(rule: InitdRule, url: string): IS_MATCH {
212212
if (detectRunner(rule) === 'dnr' && rule._re2 && ENABLE_DNR) {
213213
result = rule._re2.matches(url);
214214
} else {
215-
const reg = rule._reg || new RegExp(regex, 'g');
216-
reg.lastIndex = 0;
215+
const reg = rule._reg || new RegExp(regex);
217216
result = result && reg.test(url);
218217
}
219218
}
@@ -224,8 +223,7 @@ export function isMatchUrl(rule: InitdRule, url: string): IS_MATCH {
224223
return IS_MATCH.MATCH_BUT_EXCLUDE;
225224
}
226225
if (excludeRegex) {
227-
const reg = rule._exclude || new RegExp(excludeRegex, 'g');
228-
reg.lastIndex = 0;
226+
const reg = rule._exclude || new RegExp(excludeRegex);
229227
if (reg.test(url)) {
230228
return IS_MATCH.MATCH_BUT_EXCLUDE;
231229
}
@@ -236,7 +234,6 @@ export function isMatchUrl(rule: InitdRule, url: string): IS_MATCH {
236234
switch (rule.matchType) {
237235
case 'regexp': {
238236
const reg = rule._reg;
239-
reg.lastIndex = 0;
240237
result = reg.test(url);
241238
break;
242239
}

0 commit comments

Comments
 (0)