简体   繁体   中英

Remove HTML tags and formatting text

I would like to remove HTML tags between text and change newline to space. I'm using this pattern below but it is not perfectly. It adds two or more space between text. How to fix this pattern?

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

try below code and check

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

UPDATE

You can do this way,

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, '');

Output will be like this,

Example:   CONTENT 

Play around the above solution & you will succeed.

Here is how I'll do what you want:
(See comments in my snippet)

 // 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); 

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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