
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove html tags from a string in JavaScript?
Removing HTML tags from a string
We can remove HTML/XML tags in a string using regular expressions in javascript. HTML elements such as span, div etc. are present between left and right arrows for instance <div>,<span> etc. So replacing the content within the arrows, along with the arrows, with nothing('') can make our task easy.
Syntax
str.replace( /(<([^>]+)>)/ig, '');
Example-1
<html> <body> <script> function removeTags(str) { if ((str===null) || (str==='')) return false; else str = str.toString(); return str.replace( /(<([^>]+)>)/ig, ''); } document.write(removeTags('<html> <body> Javascript<body> is not Java'));; </script> </body> </html>
Output
Javascript is not Java
Example-2
<html> <body> <script> function removeTags(str) { if ((str===null) || (str==='')) return false; else str = str.toString(); return str.replace( /(<([^>]+)>)/ig, ''); } document.write(removeTags('<html> Tutorix is <script> the best <body> e-learning platform'));; </script> </body> </html>
Output
Tutorix is the best e-learning platform
- Related Questions & Answers
- How to remove the HTML tags from a given string in Java?
- Remove and add new HTML Tags with JavaScript?
- How to remove text from a string in JavaScript?
- How to remove “,” from a string in JavaScript
- How to use Boto3 to remove tags from AWS Glue Resources
- How to remove underline from a link in HTML?
- Remove all whitespaces from string - JavaScript
- Remove characters from a string contained in another string with JavaScript?
- HTML Deprecated Tags
- How to Remove Punctuations From a String in Python?
- How to Remove Characters from a String in Arduino?
- How to remove a particular character from a String.
- How to remove empty tags using BeautifulSoup in Python?
- Remove the whitespaces from a string using replace() in JavaScript?
- JavaScript Remove non-duplicate characters from string
Advertisements