How to check if a string is html or not using JavaScript? Last Updated : 19 Jan, 2023 Comments Improve Suggest changes 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 How to check if a string is html or not 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 How to check a URL contains a hash or not using JavaScript ? In this article, the task is to check whether an URL contains or not. This can be done by using the Location hash property in JavaScript. It returns the string which represents the anchor part of a URL including the hash â#â sign. Syntax: window.location.hash Example: This example uses the hash prop 1 min read How to detect flash is installed or not using JavaScript ? The task is to detect whether the user has installed Adobe Flash player or not with the help of JavaScript. we're going to discuss 2 techniques. Approach: Create a ShockwaveFlash.ShockwaveFlash object.If the instance's value is true, Flash is installed.If any error occurred, Use navigator.mimetypes 2 min read How to Check if an element is a child of a parent using JavaScript? In this article, we are going to see the methods by which we can Check if an element is a child of a parent using JavaScript. These are the following methods: Table of Content Using the Node.contains() methodLooping through the parents of the given childUsing the hasChildNodes() methodMethod 1: Usin 5 min read How to check if CSS property is supported in browser using JavaScript ? Few CSS properties are not supported in some browsers, thus it is required to check whether a CSS property & its value are supported in the current browser. We can find it using JavaScript using CSS.supports() method. Syntax: supports(propertyName, value) supports(condition) There are two distin 2 min read Like