Skip to content

JavaScript remove HTML tags from string regex | Example code

  • by

Use Regex to remove all the HTML tags out of a string in JavaScript. Here is the code for it:-

It will strip out all the html-tags.

regex = /(<([^>]+)>)/ig

JavaScript remove HTML tags from string regex Example code

Complete HTML example code:-

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">

       var regex = /(<([^>]+)>)/ig 
       var body = "<p>Text with html tag</p>"
       
       var result = body.replace(regex, "");

       console.log(result); 
   </script>
</head>
<body>

</body>

</html>

Output:

You can use a library such as jQuery, you could simply do this:

console.log($('<p>test</p>').text());

Do comment if you have any doubts and suggestions on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading