Search code examples
javascriptjqueryhtmlregexstrip

jQuery / Regex: remove all p tags


I am new to Regex and hope someone can help me with the following:

I have a long string that is saved as a variable. The string contains plain text and HTML tags, incl. p tags.

How can I use Regex to remove all p tags from my string including classes and IDs on the p tags but without losing the text inside ? The string variable can contain one, multiple or no p tags but always contains at least some text.

Example: How it looks now:

var str = Some awesome text with a <p class='myClass'>new paragraph</p> and more text.

How it should look:

var str = Some awesome text with a new paragraph and more text.

Thanks for any help with this, Tim.


Solution

  • result = str.replace(/(<p[^>]+?>|<p>|<\/p>)/img, "");
    

    updated regex