简体   繁体   中英

Remove line breaks if there are no characters on that line

I'm trying to remove linebreaks if there are no characters next to it. 在此处输入图片说明

I'd like to keep Line1, Line2, Line3 untouched but the break after Line3 and before Line1 removed.

I tried:

Element.value = Element.value.replace(/\n?![A-z]/g, "");

I'm not too good with regex.

Thanks.

.replace(/(\r?\n){2,}/g, '\n').replace(/^\r?\n|\r?\n$/g, '');

Demo

This regex replaces sequences of 2 or more line breaks with a single line break, then remove leading and trailing line breaks.

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