articles » current » dot-net » strip-html-script-tags-from-text-using-regex

.NET: Strip/Remove HTML SCRIPT Tags from Text Using Regex

How to remove the SCRIPT tag and its containing code from HTML text using C# and regular expressions.

See related: Find Keywords in Text and strip all HTML tags from text

It is very easy to remove all SCRIPT tags in HTML text using Regex.Replace().

Do this is JavaScript, client-side (in the browser).

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
	class Program
		static void Main(string[] args)
			var html = @"
<p>This is a test</p>

<script type='text/javascript'>

<p>The script tag should have been removed!</p>

			Console.Write(Regex.Replace(html, @"<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>", string.Empty, RegexOptions.IgnoreCase));

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.