JAVASCRIPT

Stripping Basic HTML Tags from a String

Discover how to quickly remove simple HTML tags from a string using a regular expression, perfect for cleaning text content for display purposes.

const htmlString = "<p>Hello <strong>world</strong>!</p> <script>alert('xss');</script>";
const plainText = htmlString.replace(/<[^>]*>/g, '');
console.log(plainText); // Output: "Hello world! "
How it works: This regex `/<[^>]*>/g` matches any sequence starting with `<` and ending with `>` (including the tags themselves). The `[^>]*` part matches any character except `>` zero or more times. By replacing these matches with an empty string, all basic HTML tags are removed from the text. This is typically used for display purposes, not for robust security sanitization.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs