const escape = (text, flag = 0) => { const replaceAll = (text, pattern, func) => { const…
- User
const escape = (text, flag = 0) => { const replaceAll = (text, pattern, func) => { const findAllIndex = (str, pattern) => { const indexList = [0]; const regex = new RegExp(pattern, 'gm'); let match; while ((match = regex.exec(str)) !== null) { if (match[1] !== undefined) { const start = match.index; const end = regex.lastIndex; indexList.push(start, end); } } indexList.push(str.length); return indexList; };
const poslist = findAllIndex(text, pattern); const strlist = []; const originstr = []; for (let i = 1; i < poslist.length - 1; i += 2) { const start = poslist[i]; const end = poslist[i + 1]; strlist.push(func(text.substring(start, end))); } for (let i = 0; i < poslist.length; i += 2) { const j = poslist[i]; const k = poslist[i + 1]; originstr.push(text.substring(j, k)); } if (strlist.length < originstr.length) { strlist.push(""); } else { originstr.push(""); } const newList = []; for (let i = 0; i < originstr.length; i++) { newList.push(originstr[i]); if (i < strlist.length) { newList.push(strlist[i]); } } return newList.join(""); }; const escapeshape = (text) => { return "\u258e*" + text.split(" ")[1] + "*"; }; const escapeminus = (text) => { return "\\" + text; }; const escapebackquote = (text) => { return "\\`\\`"; }; const escapeplus = (text) => { return "\\" + text; }; text = text.replace(/\\\[/g, "@->@"); text = text.replace(/\\\]/g, "@<-@"); text = text.replace(/\\\(/g, "@-->@"); text = text.replace(/\\\)/g, "@<--@"); if (flag) { text = text.replace(/\\\\/g, "@@@"); } text = text.replace(/\\/g, "\\\\"); if (flag) { text = text.replace(/@@@/g, "\\\\"); } text = text.replace(/_/g, "\\_"); text = text.replace(/\*\*(.*?)\*\*/g, "@@@$1@@@"); text = text.replace(/\n{1,2}\*\s/g, "\n\n\u2022 "); text = text.replace(/\*/g, "\\*"); text = text.replace(/@@@(.*?)@@@/g, "*$1*"); text = text.replace(/!\[(.*?)\]\((.*?)\)/g, "@@@$1@@@^^^$2^^^"); text = text.replace(/\[/g, "\\["); text = text.replace(/\]/g, "\\]"); text = text.replace(/\(/g, "\\("); text = text.replace(/\)/g, "\\)"); text = text.replace(/@->@/g, "\\["); text = text.replace(/@<-@/g, "\\]"); text = text.replace(/@-->@/g, "\\("); text = text.replace(/@<--@/g, "\\)"); text = text.replace(/@@@(.*?)@@@^^^(.*?)\^\^\^/g, "[$1]($2)"); text = text.replace(/~/g, "\\~"); text = text.replace(/>/g, "\\>"); text = replaceAll(text, /(^#+\s.+?$)|```[\D\d\s]+?```/g, escapeshape); text = text.replace(/#/g, "\\#"); text = replaceAll(text, /(\+)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`/g, escapeplus); text = text.replace(/\n{1,2}(\s*)-\s/g, "\n\n\\1\u2022 "); text = text.replace(/\n{1,2}(\s*\d{1,2}\.\s)/g, "\n\n\\1"); text = replaceAll(text, /(-)|\n[\s]*-\s|```[\D\d\s]+?```|`[\D\d\s]*?`/g, escapeminus); text = text.replace(/```([\D\d\s]+?)```/g, "@@@$1@@@"); text = replaceAll(text, /(``)/g, escapebackquote); text = text.replace(/@@@([\D\d\s]+?)@@@/g, "```$1```"); text = text.replace(/=/g, "\\="); text = text.replace(/\|/g, "\\|"); text = text.replace(/{/g, "\\{"); text = text.replace(/}/g, "\\}"); text = text.replace(/\./g, "\\."); text = text.replace(/!/g, "\\!"); return text; - Assistant