用正则表达式替换两个以上的换行符 [英] Replacing more than two line breaks with regex

查看:784
本文介绍了用正则表达式替换两个以上的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的textarea中搜索\ n换行符,但我想要两行空格。

I want to search my textarea for "\n" line breaks but I want two line spaces to be the maximum.

我可以在 regex 中使用什么公式,以便它可以查找超过三个的任何公式 \ n 连续( \ n\\\
\ n
)并用一个< br>

What formula can I use in this regex so that it looks for anything over three \n's in a row ("\n\n\n") and replaces it with just one <br> ?

this.replace(new RegExp('\n', 'gim') , '<br/>');


推荐答案

this.replace(new RegExp('(\n){3,}', 'gim') , '<br/>');

这将用br取代3个或更多\ n,如果你想要4或4那么取4更多。

This will replace 3 or more \n's with a br, make that 4 if you want 4 or more.

这篇关于用正则表达式替换两个以上的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆