How to check if a string is html or not using JavaScript?
The task is to validate whether the given string is valid HTML or not using JavaScript. we’re going to discuss few techniques.
Approach
- Get the HTML string into a variable.
- Create a RegExp which checks for the validation.
- RegExp should follow the rules of creating a HTML document.
Example 1: In this example, a regexp is created and it is validating the HTML string as valid.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Check if a string is html or not. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;" id="h1"> 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 str1 = '<div>GeeksForGeeks</div>'; var str = '<div>GeeksForGeeks</div>'; up.innerHTML = "Click on the button to check "+ "for the valid HTML.<br> String - " + str1; var down = document.getElementById('GFG_DOWN'); function GFG_Fun() { down.innerHTML = /<(?=.*? .*?\/ ?>|br|hr|input|!--|wbr)[a-z]+.*?>|<([a-z]+).*?<\/\1>/i.test(str); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: In this example, Here also, A regexp is created and it is validating the HTML string as invalid.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Check if a string is html or not. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;" id="h1"> 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 str1 = '<div>GeeksForGeeks</dv>'; var str = '<div>GeeksForGeeks</dv>'; up.innerHTML = "Click on the button to check "+ "for the valid HTML.<br> String - " + str1; var down = document.getElementById('GFG_DOWN'); function GFG_Fun() { down.innerHTML = /<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/.test(str); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Check if a variable is a string
- How to check if string contains only digits in JavaScript ?
- How to check a string is entirely made up of the same substring in JavaScript ?
- Check whether HTML element has scrollbars using JavaScript
- JavaScript | Check if a string is a valid hex color representation
- How to check empty/undefined/null string in JavaScript?
- How to get the entire HTML document as a string in JavaScript ?
- JavaScript | Difference between String.slice and String.substring
- JavaScript | Insert a string at position X of another string
- How to count string occurrence in string using JavaScript?
- PHP | Program to check a string is a rotation of another string
- Substring check in JavaScript
- Check a number is Prime or not using JavaScript
- How to check if a variable is an array in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


