[英]Remove HTML tags and formatting text
我想刪除文本之間的HTML標記,並將換行符更改為空格。 我在下面使用這種模式,但它並不完美。 它在文本之間添加兩個或多個空間。 如何解決這種模式?
replace(/( |<([^>]+)>)/ig, ' ');
嘗試下面的代碼並檢查
replace(/(<([^>]+)>)/ig,"");
UPDATE
你可以這樣
var html = 'Example: <h1></h1><p></p><div> </div><div>CONTENT</div> ';
html = html.replace(/\s|\n| /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.