
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to remove HTML Tags with RegExp in JavaScript?
The regex will identify the HTML tags and then the replace() is used to replace the tags with null string. Let’s say we have the following HTML −
<html><head></head><body><p>The tags stripped...<p</body></html>
We want to remove the above tags with Regular Expression. For that, we will create a custom function −
function removeTags(myStr)
The myStr will have our HTML code for which we want to remove the tags −
function removeTags(myStr) { if ((myStr===null) || (myStr==='')) return false; else myStr = myStr.toString(); return myStr.replace( /(<([^>]+)>)/ig, ''); }
The call for the above function to remove tags goes like this −
document.write(removeTags('<html><head></head><body><p>The tags stripped...<p</body></html>'));;
Example
Let us now see the complete example −
<!DOCTYPE html> <html> <title>Strip HTML Tags</title> <head> <script> function removeTags(myStr) { if ((myStr===null) || (myStr==='')) return false; else myStr = myStr.toString(); return myStr.replace( /(<([^>]+)>)/ig, ''); } document.write(removeTags( '<html><head></head><body><p>The tags stripped...<p</body></html>'));; </script> </head> <body> </body> </html>
Output

- Related Articles
- Remove and add new HTML Tags with JavaScript?
- How to remove html tags from a string in JavaScript?
- How to remove the HTML tags from a given string in Java?
- How to perform Multiline matching with JavaScript RegExp?
- With JavaScript RegExp how to search a string for text that matches regexp?
- How to strip out HTML tags from a string using JavaScript?
- How to remove empty tags using BeautifulSoup in Python?
- How to remove existing HTML elements in JavaScript?
- How to perform Case Insensitive matching with JavaScript RegExp?
- How can I remove the (//) blocks; tags inside a script element in Javascript?
- Find digit with JavaScript RegExp.
- How to use Boto3 to remove tags in specified AWS secrets
- How to create RegExp object in JavaScript?
- Disable browser caching with meta HTML tags
- HTML Deprecated Tags

Advertisements