IT干货网

javascript之替换所有以

thcjp 2025年05月04日 编程设计 147 0

如何替换以 #

开头的多行字符串中的所有行
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") 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!