簡體   English   中英

刪除HTML標簽和格式化文本

[英]Remove HTML tags and formatting text

我想刪除文本之間的HTML標記,並將換行符更改為空格。 我在下面使用這種模式,但它並不完美。 它在文本之間添加兩個或多個空間。 如何解決這種模式?

replace(/(&nbsp;|<([^>]+)>)/ig, ' ');

嘗試下面的代碼並檢查

replace(/(<([^>]+)>)/ig,"");

UPDATE

你可以這樣

var html = 'Example: &nbsp;<h1></h1><p></p><div>&nbsp;</div><div>CONTENT</div>&nbsp;';
html = html.replace(/\s|\n|&nbsp;/g, ' ');
html = html.replace(/<[^>]+>/gm, '');

輸出將是這樣,

Example:   CONTENT 

嘗試以上解決方案,您將成功。

這是我將要執行的操作:
(請參見我的摘錄中的評論)

 // Input data var input_data = `My<div><br> <span></span> <span></span> </div><p>Content</p>`; console.log("Input:", input_data); // Creates html element with Input data var elm = document.createElement('div'); elm.innerHTML = input_data; // Use native function '.innerText' to get rid of the html, // then replace new lines by spaces, and multiple spaces by only one space output_data = elm.innerText.replace(/\\n/g, ' ').replace(/[\\s]+/g, ' '); console.log("Output:", output_data); 

希望能幫助到你!

暫無
暫無

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

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