簡體   English   中英

如何刪除 HTML 中的多余空格或新行

[英]How to remove extra space or new line in HTML

當我致電 API 時,我收到如下回復

{
  "statusCode": 200,
  "message": null,
  "responseBody": {
    "subject": "Task2",
    "emailContent": "",
    "emailContentTrail": "Hi ,\r\nReplying\r\nFrom Outlook\r\n\r\nIn multiple\r\nLines\r\n\r\nthanks you,\r\nSam \r\n________________________________\r\nFrom: Workflow,\r\nSent: Monday, April 5, 2021 12:01 PM\r\nTo: Workflow,\r\nCc: Sam \r\nSubject:Task2\r\n\r\nCAUTION: EXTERNAL EMAIL. Please do not reply, forward, or open links/attachments unless you recognize the sender.\r\n\r\nHi ,\r\n\r\nMultiple Line\r\n\r\nMessage Checking\r\n\r\n\r\nMessage\r\n\r\n\r\nThank you,\r\n\r\nSam \r\n\r\n\r\n________________________________\r\nTry to see whether extra space can be removed using css or regex pattern\r\n\r\n\r\nDisclaimer: The information contained in this e-mail and any accompanying documents may contain information that is confidential or otherwise protected from disclosure. If you are not the intended recipient of this message, or if this message has been addressed to you in error, please immediately alert the sender by reply e-mail and then delete this message, including any attachments. Any dissemination, distribution or other use of the contents of this message by anyone other than the intended recipient is strictly prohibited. Any comments or statements made are not necessarily those of Xceedance. All messages sent to and from this e-mail address may be monitored as permitted by applicable law and regulations to ensure compliance with our internal policies and to protect our business. E-mails are not secure and cannot be guaranteed to be error free as they can be intercepted, amended, lost or destroyed, or contain viruses. You are deemed to have accepted these risks if you communicate with us by e-mail.\r\n",
    "attachments": []
  },
  "page": null,
  "links": null,
  "error": false
}

我必須在文本區域中顯示“emailContentTrail”數據。 但我想刪除存在的額外新行。 我怎樣才能做到這一點。 目前數據如下在此處輸入圖像描述

如您所見,兩個文本之間有很多空白。 我想刪除那個空間。 我怎樣才能做到這一點。

嘗試將\r\n\r\n替換為\r\n ,您可以使用以下正則表達式:

emailContentTrail.replaceAll("(\r?\n){2,}", "\r\n");

您可以使用以下代碼刪除字符串中的所有新行:

 var originalText = "This is a test text and the next \n line will be removed." var removedNewLinesText = originalText,replace(/(\r\n|\n|\r)/gm;""). console;log(originalText). console;log(removedNewLinesText);

只需在要刪除的部分上使用此方法即可。

暫無
暫無

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

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