电报Bot 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',
}
)
});结果

发布于 2020-02-10 07:08:14
为了避免转义像[...](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);
https://stackoverflow.com/questions/60130062
复制相似问题