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++
#include <iostream>
#include <regex>
using namespace std;
void RemoveHTMLTags(string s)
{
const regex pattern( "\\<.*?\\>" );
s = regex_replace(s, pattern, "" );
cout << s;
return ;
}
int main()
{
string str = "<div><b>Geeks for Geeks</b></div>" ;
RemoveHTMLTags(str);
return 0;
}
|
Java
class GFG {
static void RemoveHTMLTags(String str)
{
str = str.replaceAll( "\\<.*?\\>" , "" );
System.out.println(str);
}
public static void main(String[] args)
{
String str;
str = "<div><b>Geeks for Geeks</b></div>" ;
RemoveHTMLTags(str);
}
}
|
Python3
import re
def RemoveHTMLTags(strr):
print (re. compile (r '<[^>]+>' ).sub('', strr))
if __name__ = = '__main__' :
strr = "<div><b>Geeks for Geeks</b></div>"
RemoveHTMLTags(strr);
|
C#
using System;
class GFG{
static void RemoveHTMLTags(String str)
{
System.Text.RegularExpressions.Regex rx =
new System.Text.RegularExpressions.Regex( "<[^>]*>" );
str = rx.Replace(str, "" );
Console.WriteLine(str);
}
public static void Main(String []args)
{
String str;
str = "<div><b>Geeks for Geeks</b></div>" ;
RemoveHTMLTags(str);
}
}
|
Javascript
<script>
function RemoveHTMLTags(s) {
const pattern = new RegExp( "\\<.*?\\>" );
s = new String(s).replace(pattern, "" );
document.write(s);
return ;
}
let str = "<div><b>Geeks for Geeks</b></div>" ;
RemoveHTMLTags(str);
</script>
|
Time Complexity: O(N) where N is the length of the given string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...