Categories
JavaScript Answers

How to remove all script tags from HTML with a JavaScript regular expression?

Spread the love

Sometimes, we want to remove all script tags from HTML with a JavaScript regular expression.

In this article, we’ll look at how to remove all script tags from HTML with a JavaScript regular expression.

How to removing all script tags from HTML with a JavaScript regular expression?

To remove all script tags from HTML with a JavaScript regular expression, we can use a regex that match the script tags and its contents.

For instance, we write

/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi

to define a regex that matches the script tags and the contents inside.

We use g to find all matches.

And we use i to find matches in a case insensitive manner.

Conclusion

To remove all script tags from HTML with a JavaScript regular expression, we can use a regex that match the script tags and its contents.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *