<p>I am trying to write a regular expression to strip all HTML with the exception of links (the <code>&lt;a href</code> and <code>&lt;/a&gt;</code> tags respectively. It does not have to be 100% secure (I am not worried about injection attacks or anything as I am parsing content that has already been approved and published into a <a href="http://en.wikipedia.org/wiki/SWF" rel="noreferrer">SWF</a> movie).</p>

<p>The original "strip tags" regular expression I'm using was <code>&lt;(.|\n)+?&gt;</code>, and I tried to modify it to <code>&lt;([^a]|\n)+?&gt;</code>, but that of course will allow any tag that has an <strong>a</strong> in it rather than one that has it in the beginning, with a space.</p>

<p>Not that it should really matter, but in case anyone cares to know I am writing this in <a href="http://en.wikipedia.org/wiki/ActionScript#ActionScript_3.0" rel="noreferrer">ActionScript 3.0</a> for a <a href="http://en.wikipedia.org/wiki/Adobe_Flash" rel="noreferrer">Flash</a> movie.</p>

[{'answer_id': 44088, 'author': 'Jimmy', 'author_id': 4435, 'author_profile': 'https://Stackoverflow.com/users/4435', 'pm_score': -1, 'selected': False, 'text': '<p>How about</p>\n\n<pre><code>&lt;[^a](.|\\n)+?&gt;\n</code></pre>\n\n<p>?</p>\n'}, {'answer_id': 44124, 'author': 'Xetius', 'author_id': 274, 'author_profile': 'https://Stackoverflow.com/users/274', 'pm_score': 6, 'selected': True, 'text': "<pre><code>&lt;(?!\\/?a(?=&gt;|\\s.*&gt;))\\/?.*?&gt;\n</code></pre>\n\n<p>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 </p>\n\n<pre><code>s/&lt;(?!\\/?a(?=&gt;|\\s.*&gt;))\\/?.*?&gt;//g;\n</code></pre>\n\n<p>This should leave only the opening and closing a tags</p>\n"}, {'answer_id': 45689, 'author': 'grapefrukt', 'author_id': 914, 'author_profile': 'https://Stackoverflow.com/users/914', 'pm_score': 0, 'selected': False, 'text': '<p>I keep going on about it, but there\'s no way I can recommend <a href="http://www.gskinner.com/RegExr/" rel="nofollow noreferrer">regexr</a> too often. It\'s fantastic for testing this type of things.</p>\n'}, {'answer_id': 114349, 'author': 'domgblackwell', 'author_id': 16954, 'author_profile': 'https://Stackoverflow.com/users/16954', 'pm_score': 1, 'selected': False, 'text': "<p>In general there are problems with this approach. Regexes are best for 'flat' text matches - nested data pushes regex engines into areas for which they are not designed. General HTML parsing needs a parser not a regex engine (Google for the difference between regular and context-free languages if you want the full technical details). </p>\n\n<p>It is easy to strip out all tags by replacing /&lt;/ and /&gt;/ with the empty string or their entity equivalents but selectively filtering HTML using regexes will be vulnerable to a wide range of accidental or malicious inputs breaking things.</p>\n"}, {'answer_id': 1968498, 'author': 'Qamar ', 'author_id': 237660, 'author_profile': 'https://Stackoverflow.com/users/237660', 'pm_score': 0, 'selected': False, 'text': '<p>Here you go:</p>\n\n<pre><code>{&lt;(?!i|b|h[1-6]|/i|/b|/h[1-6][\\s|&gt;|/])[^&gt;]*&gt;}\n</code></pre>\n'}, {'answer_id': 23640549, 'author': 'Geremia', 'author_id': 1429450, 'author_profile': 'https://Stackoverflow.com/users/1429450', 'pm_score': -1, 'selected': False, 'text': '<p><a href="http://php.net/strip_tags" rel="nofollow"><code>strip_tags()</code></a> does this.</p>\n\n<p>Here, I am including all <code>&lt;a&gt;&lt;p&gt;&lt;font&gt;&lt;b&gt;&lt;i&gt;&lt;sup&gt;</code> tags and outputting a tidied version:</p>\n\n<pre><code>cat input.htm | tr -d \'\\n\' | php -r \'$input=fgets(STDIN); echo strip_tags($input,"&lt;a&gt;&lt;p&gt;&lt;font&gt;&lt;b&gt;&lt;i&gt;&lt;sup&gt;");\' | tidy -i -wrap 0 -o output.htm\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/44078', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1306/']