Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to remove HTML tags with RegExp in JavaScript?

  • Last Updated : 06 Sep, 2019

Here, the task is to remove the HTML tags from the string. Here string contains a part of the document and we need to extract only the text part from it. Here we are going to do that with the help of JavaScript.

Approach:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Take the string in a variable.
  • Anything between the less than symbol and the greater than symbol is removed from the string by the RegExp.
  • Finally we will get the text.

Example 1: This example using the approach defined above.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to remove HTML tags with RegExp in JavaScript?
    </title>
</head>
  
<body id="body" style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px;
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color:green;
              font-size: 20px;
              font-weight: bold;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var str2 = '<p> GeeksForGeeks </p>';
        
        // same as the str2. 
        var str1 = "< p > GeeksForGeeks < /p >"; 
        up.innerHTML = "Click on button to remove the "+
          "HTML tags from the string.<br>String = '" + str1 + "'";
        var down = document.getElementById('GFG_DOWN');
  
        function GFG_Fun() {
            var regex = /( |<([^>]+)>)/ig;
            down.innerHTML = str2.replace(regex, "");
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: This example using the approach defined above but by a different RegExp.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to remove HTML tags 
      with RegExp in JavaScript?
    </title>
</head>
  
<body id="body" style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px;
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color:green; 
              font-size: 20px;
              font-weight: bold;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var str2 = '<p> GeeksForGeeks </p>';
         
        // same as the str2.
        var str1 = "< p > GeeksForGeeks < /p >";  
        up.innerHTML = "Click on button to remove the HTML"+
          " tags from the string.<br>String = '" + str1 + "'";
        var down = document.getElementById('GFG_DOWN');
  
        function GFG_Fun() {
            var regex = /(<([^>]+)>)/ig;
            down.innerHTML = str2.replace(regex, "");
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!