Remove HTML Tags From String JavaScript
Last Updated : Mar 11, 2024
IN - HTML JavaScript | Written & Updated By - Dikshita
In this tutorial we will show you the solution of remove HTML tags from string JavaScript, mainly there are many methods with help of which we are able to remove tags. Let us understand them one by one.
As, there are methods like replace(), textcontent() and innerText(). These all are inbuilt or predefined function with which we can remove tags.
Step By Step Guide On Remove HTML Tags From String JavaScript :-
In this article we are going to use replace() to remove tags from string. In next sessions we will understand other methods and understand how they work. Let us see the codes below to understand replace().
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>convert object to array using JavaScript</title> </head> <body> <script> function removeHtml(string) { if ((string===null) || (string==='')) return false; else string = string.toString(); return string.replace( /(<([^>]+)>)/ig, ''); } document.write(removeHtml( '<html> Hello World! </html>')); </script> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
- Here, as we can see that we directly use script tag to write our JavaScript code there and as we know that JavaScript is written under script tag, script tag is a paired tag and must have a closing tag.
- Here, we create a function with name removeHtml and pass an argument string there. If our string is null or empty it return false else the function runs.
- This all we done with help of if condition. In else our string is converted to string format using toString() and then we use striing.replace() to remove the html tags under JavaScript.
- After that we use our function removeHtml that we created above and pass a string inside this.
- We you see the output you see only Hello World! You see that the html tags that we created there gets removed. We hope that you understand the codes properly with help of these steps.
- At last, the <body> and <html> tags are closed with </body> and </html> respectively.
Conclusion :-
At last in conclusion, here we can say that with the help of this article we are able to understand how to remove tags from string JavaScript.
I hope this tutorial on remove HTML tags from string JavaScript helps you and the steps and method mentioned above are easy to follow and implement.