mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-30 15:40:22 +08:00
1 parent
d4a0bb572f
commit
111427b198
8 files changed
+59
-35
No files matched your search
+11
-4
@@ -3577,11 +3577,16 @@ function patchApplied(text, patch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function cloneRegExp(pattern, global) {
|
||||
let flags = String(pattern.flags || "").replace("g", "");
|
||||
if (global) flags += "g";
|
||||
return new RegExp(pattern.source, flags);
|
||||
}
|
||||
|
||||
function matchesPattern(hay, pattern) {
|
||||
if (pattern instanceof RegExp) {
|
||||
const re = pattern.global ? pattern : new RegExp(pattern.source, pattern.flags);
|
||||
re.lastIndex = 0;
|
||||
return re.test(hay);
|
||||
// 不复用带 g 的模块级正则。test() 会留下 lastIndex,下一次替换从中间开始,等于没匹配到。
|
||||
return cloneRegExp(pattern, false).test(hay);
|
||||
}
|
||||
return hay.includes(asLf(pattern));
|
||||
}
|
||||
@@ -3655,8 +3660,10 @@ function applyReplacementsToText(text, patch, fp = "") {
|
||||
if (yaml && isBareIdentityNeedle(pattern)) continue;
|
||||
// 正则 pattern:用于自匹配型替换(产物包含原 pattern,如 timeoutMs 追加 0)。
|
||||
if (pattern instanceof RegExp) {
|
||||
const re = pattern.global ? pattern : new RegExp(pattern.source, pattern.flags + "g");
|
||||
const re = cloneRegExp(pattern, true);
|
||||
re.lastIndex = 0;
|
||||
if (!re.test(out)) continue;
|
||||
re.lastIndex = 0;
|
||||
// 用字符串形态调用:保留 $1..$9 展开语义(回调形态会把 "$1" 当字面量写进文件)。
|
||||
const next = out.replace(re, asLf(replace));
|
||||
if (next === out) continue;
|
||||
|
||||
Reference in new issue
Block a user