Strip all HTML tags except p tag

Stripping HTML tags using JavaScript is not always accurate because HTML is not properly formatted most of the time. We can easily strip html tags using JavaScript. Following the JavaScript code snippet that we can use to strip HTML except <p> tag only.

In one of my project I needed to remove all the HTML tags but to keep p tag from a chunk of HTML. I solved it using regular expression. Following is the regular expression that I have used to do it:
html.replace(/<\s*\/?\s*(?!p)\w\s*.*?>/g, '');

Comments