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 one double quotes string, one single quotes string or a closing tag (>) without single or double quotes enclosed.
- It should end with a closing tag (>).
Examples:
Input: str = “<input value = ‘>’>”;
Output: true
Explanation: The given string satisfies all the above mentioned conditions.
Input: str = “<br/>”;
Output: true
Explanation: The given string satisfies all the above mentioned conditions.
Input: str = “br/>”;
Output: false
Explanation: The given string doesn’t starts with an opening tag “<“. Therefore, it is not a valid HTML tag.
Input: str = “<‘br/>”;
Output: false
Explanation: The given string has one single quotes string that is not allowed. Therefore, it is not a valid HTML tag.
Input: str = “<input value => >”;
Output: false
Explanation: The given string has a closing tag (>) without single or double quotes enclosed that is not allowed. Therefore, it is not a valid HTML tag.
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.
- Get the String.
- Create a regular expression to check valid HTML tag as mentioned below:
regex = “<(“[^”]*”|'[^’]*’|[^'”>])*>”;
- Where:
- < represents the string should start with an opening tag (<).
- ( represents the starting of the group.
- “[^”]*” represents the string should allow double quotes enclosed string.
- | represents or.
- ‘[^’]*‘ represents the string should allow single quotes enclosed string.
- | represents or.
- [^'”>] represents the string should not contain one single quote, double quotes, and “>”.
- ) represents the ending of the group.
- * represents 0 or more.
- > represents the string should end with a closing tag (>).
- Match the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidHTMLTag(string str)
{
const regex pattern( "<(\"[^\"]*\"|'[^']*'|[^'\">])*>" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "<input value = '>'>" ;
cout << ((isValidHTMLTag(str1)) ? "true" : "false" ) << endl;
string str2 = "<br/>" ;
cout << ((isValidHTMLTag(str2)) ? "true" : "false" ) << endl;
string str3 = "br/>" ;
cout << ((isValidHTMLTag(str3)) ? "true" : "false" )<< endl;
string str4 = "<'br/>" ;
cout << ((isValidHTMLTag(str4))? "true" : "false" ) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean
isValidHTMLTag(String str)
{
String regex
= "<(\"[^\"]*\"|'[^']*'|[^'\">])*>" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "<input value = '>'>" ;
System.out.println(isValidHTMLTag(str1));
String str2 = "<br/>" ;
System.out.println(isValidHTMLTag(str2));
String str3 = "br/>" ;
System.out.println(isValidHTMLTag(str3));
String str4 = "<'br/>" ;
System.out.println(isValidHTMLTag(str4));
}
}
|
Python3
import re
def isValidHTMLTag( str ):
regex = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "<input value = '>'>"
print (isValidHTMLTag(str1))
str2 = "<br/>"
print (isValidHTMLTag(str2))
str3 = "br/>"
print (isValidHTMLTag(str3))
str4 = "<'br/>"
print (isValidHTMLTag(str4))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
public static bool
isValidHTMLTag( string str)
{
string regex
= "<(\"[^\"]*\"|'[^']*'|[^'\">])*>" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "<input value = '>'>" ;
Console.WriteLine(isValidHTMLTag(str1));
string str2 = "<br/>" ;
Console.WriteLine(isValidHTMLTag(str2));
string str3 = "br/>" ;
Console.WriteLine(isValidHTMLTag(str3));
string str4 = "<'br/>" ;
Console.WriteLine(isValidHTMLTag(str4));
}
}
|
Javascript
function isValidHTMLTag(str)
{
let regex = new RegExp(/<(\ "[^\"]*\"|'[^']*'|[^'\">])*>/);
// If the string is empty
// return false
if (str == null) {
return " false ";
}
// Find match between given string
// and regular expression
if (regex.test(str) == true) {
return " true ";
}
else {
return " false ";
}
}
// Driver Code.
// Test Case 1:
let str1 = " <input value = '>' > ";
console.log(isValidHTMLTag(str1)+" <br> ");
// Test Case 2:
let str2 = " <br/> ";
console.log(isValidHTMLTag(str2)+" <br> ");
// Test Case 3:
let str3 = " br/> ";
console.log(isValidHTMLTag(str3)+" <br> ");
// Test Case 4:
let str4 = " <'br/> ";
console.log(isValidHTMLTag(str4)+" <br>");
|
Output:
true
true
false
false
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Last Updated :
03 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...