perf: tool promot and reg slice;query extension prompt (#1576)

This commit is contained in:
Archer
2024-05-23 15:14:22 +08:00
committed by GitHub
parent 4eb2c9bd07
commit c4ce1236ea
6 changed files with 85 additions and 45 deletions

View File

@@ -64,4 +64,22 @@ export const getNanoid = (size = 12) => {
return `${firstChar}${randomsStr}`;
};
/* Custom text to reg, need to replace special chats */
export const replaceRegChars = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
/* slice json str */
export const sliceJsonStr = (str: string) => {
str = str.replace(/(\\n|\\)/g, '').replace(/ /g, '');
const jsonRegex = /{[^{}]*}/g;
const matches = str.match(jsonRegex);
if (!matches) {
return '';
}
// 找到第一个完整的 JSON 字符串
const jsonStr = matches[0];
return jsonStr;
};