简体   繁体   中英

regex remove white space and new lines

I'm trying to reduce the amount of new lines that a user can enter. If a user enters 3 or more new lines, it will replace to 2 <br> 's -

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

Problem is that if there's a space, or a tab etc. in between some br's then this regex will not match, so users can put \\n\\n space \\n\\n and it will look as though its 4 lines.

How do I change this regex, perhaps with a look forward / back to prevent this?

Thanks

I think this should work :

txt = txt.replace(/(\n[\t ]*){2,}\n/gm , '<br/><br/>')

It would replace any group of at least 3 \\n with any number of \\t and spaces in between.

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