简体   繁体   English

如果该行上没有字符,请删除换行符

[英]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. 我想保持Line1,Line2,Line3不变,但是在Line3之后和Line1删除之前的中断。

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. 此正则表达式用单个换行符替换2个或更多换行符的序列,然后删除前导和尾随的换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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