Program to remove HTML tags from a given String
Last Updated :
21 Dec, 2022
Given a string str that contains some HTML tags, the task is to remove all the tags present in the given string str.
Examples:
Input: str = "<div><b>Geeks for Geeks</b></div>"
Output: Geeks for Geeks
Input: str = "<a href="https://www.geeksforgeeks.org/">GFG</a>"
Output: GFG
Approach:
The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the resultant string:
- Get the string.
- Since every HTML tags are enclosed in angular brackets(<>). Therefore use replaceAll() function in regex to replace every substring start with "<" and ending with ">" to an empty string.
- The function is used as:
String str;
str.replaceAll("\\", "");
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <iostream>
#include <regex>
using namespace std;
// Function to remove the HTML tags
// from the given string
void RemoveHTMLTags(string s)
{
const regex pattern("\\<.*?\\>");
// Use regex_replace function in regex
// to erase every tags enclosed in <>
s = regex_replace(s, pattern, "");
// Print string after removing tags
cout << s;
return;
}
// Driver Code
int main()
{
// Given String
string str = "<div><b>Geeks for Geeks</b></div>";
// Function call to print the
// HTML string after removing tags
RemoveHTMLTags(str);
return 0;
}
// This code is contributed by yuvraj_chandra
Java
// Java program for the above approach
class GFG {
// Function to remove the HTML tags
// from the given tags
static void RemoveHTMLTags(String str)
{
// Use replaceAll function in regex
// to erase every tags enclosed in <>
str = str.replaceAll("\\<.*?\\>", "");
// Print string after removing tags
System.out.println(str);
}
// Driver Code
public static void main(String[] args)
{
String str;
// Given String
str = "<div><b>Geeks for Geeks</b></div>";
// Function call to print the
// HTML string after removing tags
RemoveHTMLTags(str);
}
}
Python3
# Python3 program for the
# above approach
import re
# Function to remove the HTML tags
# from the given tags
def RemoveHTMLTags(strr):
# Print string after removing tags
print(re.compile(r'<[^>]+>').sub('', strr))
# Driver code
if __name__=='__main__':
# Given String
strr = "<div><b>Geeks for Geeks</b></div>"
# Function call to print the HTML
# string after removing tags
RemoveHTMLTags(strr);
# This code is contributed by vikas_g
C#
// C# program for the above approach
using System;
class GFG{
// Function to remove the HTML tags
// from the given tags
static void RemoveHTMLTags(String str)
{
// Use replaceAll function in regex
// to erase every tags enclosed in <>
// str = Regex.Replace(str, "<.*?>", String.Empty)
System.Text.RegularExpressions.Regex rx =
new System.Text.RegularExpressions.Regex("<[^>]*>");
str = rx.Replace(str, "");
// Print string after removing tags
Console.WriteLine(str);
}
// Driver code
public static void Main(String []args)
{
String str;
// Given String
str = "<div><b>Geeks for Geeks</b></div>";
// Function call to print the
// HTML string after removing tags
RemoveHTMLTags(str);
}
}
// This code is contributed by vikas_g
JavaScript
<script>
// JavaScript program for the above approach
// Function to remove the HTML tags
// from the given string
function RemoveHTMLTags(s) {
const pattern = new RegExp("\\<.*?\\>");
// Use regex_replace function in regex
// to erase every tags enclosed in <>
s = new String(s).replace(pattern, "");
// Print string after removing tags
document.write(s);
return;
}
// Driver Code
// Given String
let str = "<div><b>Geeks for Geeks</b></div>";
// Function call to print the
// HTML string after removing tags
RemoveHTMLTags(str);
</script>
Time Complexity: O(N) where N is the length of the given string.
Auxiliary Space: O(1)
Similar Reads
How to remove white spaces from a string using jQuery ? In this article, we will see how to remove the white spaces from string using jQuery. To remove the white spaces, we will use trim() method. The trim() method is used to remove the white spaces from the beginning and end of a string. Syntax: jQuery.trim( str ) Parameter: This method accepts a single
1 min read
Find the missing end tag in the given HTML Code Given a string htmlCode which is HTML code of a webpage, the task is to find the missing end tag in the HTML code.Examples: Input: htmlCode = "<!DOCTYPE html><html><head> <title> GeeksforGeeks </title></head><body> <button></body></html>"Ou
9 min read
How to validate HTML tag using Regular Expression Given string str, the task is to check whether it is a valid HTML tag or not by using Regular Expression.The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<).It should be followed by a double quotes string or single quotes string.It should not allow on
6 min read
How to Display HTML Tags as Plain Text in HTML? In HTML, certain characters like <, >, ", and & are reserved for defining HTML elements and attributes. To display these characters as plain text instead of interpreting them as part of the HTML structure, you need to use special codes called HTML entities.HTML Entities for Special Charact
2 min read
What are HTML Tags ? HTML (HyperText Markup Language) is the standard markup language used to create the structure and layout of web pages. HTML documents consist of a series of elements, and these elements are defined using HTML tags. HTML tags are essential building blocks that define the structure and content of a we
2 min read