gpt4 book ai didi

regex - Telegram Bot API 4.5 MarkdownV2 上的转义字符给超链接带来麻烦

转载 作者:行者123 更新时间:2023-12-04 14:16:03 42 4
gpt4 key购买 nike

Telegram Bot API 4.5 带有新的解析模式 MarkdownV2。同时,这些 _ * [ ] ( ) ~ > # + - = | { } . ! 字符必须与前面的字符 \ 一起转义。
.replace(/[-.+?^$[\](){}\\]/g, '\\$&') 用作添加转义字符的解决方案,效果很好,但不幸的是,该解决方案确实影响了超链接方法 [inline URL](http://www.example.com/),因为它替换了 \[inline URL\]\(http://www.example\.com/\)
解决方案

bot.on('text', (ctx) => {
const { chat } = ctx.message;
const msgs = `Here is the [rules](https://telegra.ph/rules-05-06) Please read carefully and give the details which mentioned below.
*Name:*
*Place:*
*Education:*
*Experience:*
You can also call me on (01234567890)
__For premium service please contact with admin__`;

const msgmsgWithEscape = msgs.replace(/[-.+?^$[\](){}\\]/g, '\\$&')

ctx.telegram.sendMessage(
chat.id,
msgmsgWithEscape,
{
parse_mode: 'MarkdownV2',
}
)
});

结果

enter image description here

最佳答案

为了避免转义格式为 [...](http...) 的链接,您可以将它们匹配并捕获到一个组中,然后匹配所有字符以在其他上下文中转义。然后,检查 Group 1 值,如果它不为空,则替换为 Group 1 值,否则,替换为转义字符:

const msgs = `Here is the [rules](https://telegra.ph/rules-05-06) Please read carefully and give the details which mentioned below.
*Name:*
*Place:*
*Education:*
*Experience:*
You can also call me on (01234567890)
__For premium service please contact with admin__`;

const msgmsgWithEscape = msgs.replace(/(\[[^\][]*]\(http[^()]*\))|[_*[\]()~>#+=|{}.!-]/gi,
(x,y) => y ? y : '\\' + x)

console.log(msgmsgWithEscape);

关于regex - Telegram Bot API 4.5 MarkdownV2 上的转义字符给超链接带来麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60130062/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com