运用regex pipeline插件可实现整篇文档的替换,那如果想在不同标题下运用不同正则,则只能分别选中各标题下内容分别应用不同正则。能否通过templaterjs一步完成
本人代码小白,问了几家ai没能得到满足需求的代码,大佬们能帮忙写个代码吗。
下面是我问ai的其中一个代码,仅能对第一个标题正则替换,而且还会把原文复制成两份
<%*
// 获取文件内容
let fileContent = tp.file.content;
// 获取标题1部分的内容
let 1Section = fileContent.match(/(?<=^## 标题1[\r\n]+)[\s\S]?(?=\n##|$)/m)?.[0];
if (1Section) {
const 1ReplaceMap = {
“xggcg”: “1”,
“dgfjgj”: “2”,
“gxchh”: “3”
};
1Section = 1Section.replace(
new RegExp(Object.keys(1ReplaceMap).join(“|”), “g”),
match => 1ReplaceMap[match]
);
fileContent = fileContent.replace(/(?<=^## 标题1[\r\n]+)[\s\S]?(?=\n##|$)/m, 1Section);
}
// 获取标题2部分的内容
let 2Section = fileContent.match(/(?<=^## 标题2[\r\n]+)[\s\S]?(?=\n##|$)/m)?.[0];
if (2Section) {
const 2ReplaceMap = {
“dvjshjs”: “JG”,
“ytznue”: “ZJ”
“xggcg”: “c”,
“dgfjgj”: “d”,
“gxchh”: “e”
};
2Section = 2Section.replace(
new RegExp(Object.keys(2ReplaceMap).join(“|”), “g”),
match => 2ReplaceMap[match]
);
fileContent = fileContent.replace(/(?<=^## 标题2[\r\n]+)[\s\S]?(?=\n##|$)/m, 2Section);
}
// 更新最终替换后的内容
tR = fileContent;
%>