How to check if a string is html or not using JavaScript? Last Updated : 19 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The task is to validate whether the given string is valid HTML or not using JavaScript. we're going to discuss a 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 an HTML document. Example 1: In this example, a regexp is created and it is validating the HTML string as valid. html <h1 style="color:green;" id="h1"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"> </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> Output: How to check if a string is html or not using JavaScript? Example 2: In this example, Here also, A regexp is created and it is validating the HTML string as invalid. html <h1 style="color:green;" id="h1"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"> </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> Output: How to check if a string is html or not using JavaScript? Comment More infoAdvertise with us Next Article Check if a variable is a string using JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Check if a Given String is Binary String or Not in JavaScript Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string.Example:Input: "101010"Output: True, bin 3 min read How to check a string data type is present in array using JavaScript ? In JavaScript, an array is a collection of data that can be of the same or different type. If we have the array containing the data, our task is to identify if it is a string data type. In this article, we will learn how to use the typeof operator. Syntax: typeof value Note: The typeof operator retu 3 min read How to remove HTML tags from a string using JavaScript ? Removing HTML tags from a string in JavaScript means stripping out the markup elements, leaving only the plain text content. This is typically done to sanitize user input or to extract readable text from HTML code, ensuring no unwanted tags remain in the string.HTML tags come in two forms: opening t 3 min read How to check the given string is palindrome using JavaScript ? A palindrome is a word, sentence, or even number that reads the same from the back and from the front. Therefore if we take the input, reverse the string and check if the reversed string and the original string are equal, it means the string is a palindrome, otherwise, it is not. Approach: When the 3 min read Check if a variable is a string using JavaScript Checking if a variable is a string in JavaScript is a common task to ensure that the data type of a variable is what you expect. This is particularly important when handling user inputs or working with dynamic data, where type validation helps prevent errors and ensures reliable code execution.Below 3 min read How to check whether a given string is an absolute URL or not in JavaScript? We will learn how to return true if the given string is an absolute URL in JavaScript. There are two types of URLs either relative or absolute.Absolute URL: It is a URL that contains all the information that is important for locating the resources. The thing that is included in the absolute URL is t 3 min read Like