电报机器人 API 4.5带有新的解析模式 MarkdownV2。同时这些_ * [ ] ( ) ~ > # + - = | { } . !
字符必须与前面的字符一起转义\
.
.replace(/[-.+?^$[\](){}\\]/g, '\\$&')
用作添加转义字符的解决方案,效果非常好,但不幸的是,该解决方案确实影响超链接方法[inline URL](http://www.example.com/)
因为它取代了\[inline URL\]\(http://www.example\.com/\)
Solution
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',
}
)
});
Result
避免转义链接格式如下[...](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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)