简体   繁体   中英

Replacing more than two line breaks with regex

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

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。

var newString = "some \n\n\n\n\n string".replace(/\n{3,}/g, '<br/>');

alert(newString);

Did you try this?

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

You can avoid using RegExp with:

this.replace(/\\n+/g, '<br />')

this.replace(/[\n]{3,}/g,'<br/>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM