簡體   English   中英

正則表達式刪除空行,除非存在多行

[英]Regex to remove blank lines, unless multi-line is present

我正在尋找幫助,以允許我的用戶縮小某些文本輸出的格式,以適應他們的喜好。

例:

User list Summary:


Title:
- Big Pink

Student:
- Philip J Fry

Grade:
- A

Issues:
- This issue
- That issue

Improvements:
- This thing
- That thing
- The other thing

我希望能夠將單個答案縮短到一行,但不要理會多行答案。 這樣,上面的內容變為:

User list Summary:

Title: Big Pink
Student: Philip J Fry
Grade: A
Issues:
- This issue
- That issue
Improvements:
- This thing
- That thing
- The other thing

我已經做到了: https : //jsfiddle.net/hematogones/0d4g0akh/

謝謝你的幫助!

您可以使用此方法,使用負前瞻性確保沒有其他人跟隨時,僅在冒號后折疊連字符的行:

text.replace(/:\r?\n-\h*(.*)$(?!\r?\n-)/gm, ": $1" ).replace(/^\s*\r?\n/gm,'');

 const text = $("#outPut").val(); $(".switch").on("click", function(){ var text_new = ""; if (this.id == "switchto"){ text_new = text.replace(/:\\r?\\n-\\h*(.*)$(?!\\r?\\n-)/gm, ": $1" ).replace(/^\\s*\\r?\\n/gm,''); $("#outPut").val(text_new); } if (this.id == "switchback"){ $("#outPut").val(text); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="outPut" style="width:60%; min-height:180px"> User list Summary: Title: - Big Pink Student: - Philip J Fry Grade: - A Issues: - This issue - That issue Improvements: - This thing - That thing - The other thing + Final score: - 78% </textarea> <br> <input type="button" class="switch" id="switchto" value="Switch synoptic"> <input type="button" class="switch" id="switchback" value="Switch back"> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM