You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用正则将Markdown转HTML时,如何移除残留的粗体双星号?

Fixing Markdown Bold to HTML Conversion

Key Issue

Your existing regex uses lookaheads/lookbehinds which only check for the presence of ** but don't include them in the match to replace. That's why the delimiters stay in the output.

Corrected Code

Use a regex that explicitly matches the ** delimiters and captures the content between them:

const text = 'My **bold\n\n** text.\n';
const boldRegex = /\*\*(.*?)\*\*/gs;
const html = text.replace(boldRegex, '<strong>$1</strong>');
console.log(html);

Breakdown of the Regex

  • \*\*: Matches both the opening and closing bold markers
  • (.*?): Non-greedy capture group to get all content between the markers (works across newlines thanks to the s flag)
  • gs flags:
    • g: Ensures all occurrences are replaced, not just the first
    • s: Makes the . character match newline characters, so multi-line bold content is handled correctly

Result

The output will now be: My <strong>bold\n\n</strong> text. with no residual ** delimiters.

内容的提问来源于stack exchange,提问作者Anton Olshanetsky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 07:10:31