Tim
Tim

Reputation: 7056

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

Upvotes: 1

Views: 1343

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382504

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.

Upvotes: 3

Related Questions