Regex to Match All HTML Tags Except <P> and </P>

Regex to match all HTML tags except p and /p

I came up with this:

<(?!\/?p(?=>|\s.*>))\/?.*?>

x/
< # Match open angle bracket
(?! # Negative lookahead (Not matching and not consuming)
\/? # 0 or 1 /
p # p
(?= # Positive lookahead (Matching and not consuming)
> # > - No attributes
| # or
\s # whitespace
.* # anything up to
> # close angle brackets - with attributes
) # close positive lookahead
) # close negative lookahead
# if we have got this far then we don't match
# a p tag or closing p tag
# with or without attributes
\/? # optional close tag symbol (/)
.*? # and anything up to
> # first closing tag
/

This will now deal with p tags with or without attributes and the closing p tags, but will match pre and similar tags, with or without attributes.

It doesn't strip out attributes, but my source data does not put them in. I may change this later to do this, but this will suffice for now.

Javascript replace regex all html tags except p,a and img

You may match the tags to keep in a capture group and then, using alternation, all other tags. Then replace with $1:

(<\/?(?:a|p|img)[^>]*>)|<[^>]+>

Demo: https://regex101.com/r/Sm4Azv/2

And the JavaScript demo:

var input = 'b<body>b a<a>a h1<h1>h1 p<p>p p</p>p img<img />img';var output = input.replace(/(<\/?(?:a|p|img)[^>]*>)|<[^>]+>/ig, '$1');console.log(output);

Regexp to remove all html tags except br

I ended up using :

.replace('<br>','%br%').replace(/(<([^>]+)>)/g,'')

then I split on the '%br%' instead of the regular br tag.
It is not an HTML parser, I am sure it will fail to parse 100% of the World Wide Web, and it solves my particular problem 100% of the time (just tried and tested).

RegEx match open tags except XHTML self-contained tags