如何替换以 # 开头的多行字符串中的所有行

str.replace(/^#([^\n]*)\n$/gm, '<h1>$1</h1>')

多行字符串
# headline
some text

# new headline
some more text

结果字符串
<h1>headline</h1>
some text

<h1>new headline</h1>
some more text

最佳答案

像这样试试这个正则表达式 /^#(.*)$/mg

str.replace(/^#(.*)$/mg,"<h1>$1</h1")

关于javascript - 替换以开头的所有行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20047814/

10-16 04:12