简体   繁体   English

仅删除空换行符,保留空格

[英]Removing only empty line break, preserving spaces

I'm trying to remove extra line breaks, so that texts are one after another, while preserving white spaces (used as indents) 我正在尝试删除多余的换行符,以使文本一个接一个,同时保留空白(用作缩进)

I have the following: 我有以下内容:

report_text = report_text.split("\n").map($.trim).filter(function(line) { return line != "" }).join("\n");

which will return with lines of text one after another (which is correct), but will also remove my indents (two or more white spaces) because of the $.trim 这将返回一行一行的文本(这是正确的),但由于$.trim ,还将删除我的缩进(两个或更多空格)

I don't know of any other way to keep my "indents" and just only remove empty line breaks. 我不知道有什么其他方法可以保留我的“缩进”, 删除空的换行符即可。 Is there a way I can have each line of line string one after another, but preserve all my white spaces. 有没有一种方法可以让一行的每一行接一个,但保留我所有的空白。

简单的report_text.replace(/\\n+/g, "\\n")怎么样?

Also you can use this version: 您也可以使用以下版本:

report_text.replace(/(\n)+/g, "$1")

For more information read this article: 'RegExp Capturing Groups' . 有关更多信息,请阅读本文: “ RegExp捕获组”

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

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