Strip all HTML tags except links
<(?!\/?a(?=>|\s.*>))\/?.*?>
Try this. Had something similar for p tags. Worked for them so don't see why not. Uses negative lookahead to check that it doesn't match a (prefixed with an optional / character) where (using positive lookahead) a (with optional / prefix) is followed by a > or a space, stuff and then >. This then matches up until the next > character. Put this in a subst with
s/<(?!\/?a(?=>|\s.*>))\/?.*?>//g;
This should leave only the opening and closing a tags
Strip all HTML tags, except allowed
strip_tags()
does exactly this.
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);
Strip all HTML tags, except anchor tags
I suggest you use Html Agility Pack
also check this question/answers: HTML Agility Pack strip tags NOT IN whitelist
Strip all HTML tags except certain ones?
<((?!p\s).)*?>
will give you all tags except the paragraphs. So your program could delete all matches of this regex and replace the rest of the tags (all p's) with empty paragraph tags. (<p .*?>
regex for receiving all p-tags)
How can I Strip all regular html tags except a /a , img (attributes inside) and br with javascript?
Does this do what you want? http://jsfiddle.net/smerny/r7vhd/
$("body").find("*").not("a,img,br").each(function() {
$(this).replaceWith(this.innerHTML);
});
Basically select everything except a, img, br and replace them with their content.
jQuery remove all HTML tags EXCEPT Anchors
this.html(this.html().replace(/<\/?([b-z]+)[^>]*>/gi, function(match, tag) {
return (tag === "a") ? match : "";
}));
If you are looking at leaving the "a" tags in place, change the regular expression from [a-z]
to [b-z]
Related Topics
Aligning Elements Left and Center with Flexbox
Placing a <Div> Within a <Canvas>
Half Circle with CSS (Border, Outline Only)
Image Label for Input in a Form Not Clickable in Ie11
How to Preload a Page Using HTML5
Center Form Submit Buttons HTML/Css
Html5 Placeholder Disappears on Focus
How to Prevent Chrome from Changing Font When Autofilling Username/Password
How to Add Border to a Container with Transparent Gaps in Between
Cross-Browser Embedded Font in Svg Image
How to Center Div Vertically Inside of Absolutely Positioned Parent Div
Making 'File' Input Element Mandatory (Required)
Horizontally and Vertically Center a Pre Tag Without the Use of Tables