
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
html-comment-regex
Advanced tools
The html-comment-regex npm package provides a regular expression to match HTML comments. This can be useful for parsing, extracting, or removing comments from HTML content.
Match HTML Comments
This feature allows you to match all HTML comments within a given HTML string using the provided regular expression.
const htmlCommentRegex = require('html-comment-regex');
const html = '<!-- This is a comment --> <div>Content</div> <!-- Another comment -->';
const comments = html.match(htmlCommentRegex);
console.log(comments); // ['<!-- This is a comment -->', '<!-- Another comment -->']
Remove HTML Comments
This feature demonstrates how to remove all HTML comments from a given HTML string by using the regular expression to replace comments with an empty string.
const htmlCommentRegex = require('html-comment-regex');
const html = '<!-- This is a comment --> <div>Content</div> <!-- Another comment -->';
const cleanedHtml = html.replace(htmlCommentRegex, '');
console.log(cleanedHtml); // ' <div>Content</div> '
htmlparser2 is a fast and forgiving HTML/XML parser. It can be used to parse HTML and extract comments, among other elements. Unlike html-comment-regex, which uses regular expressions, htmlparser2 provides a more robust parsing solution that can handle malformed HTML.
Cheerio is a fast, flexible, and lean implementation of core jQuery designed specifically for the server. It can be used to parse HTML and manipulate the DOM, including extracting and removing comments. Cheerio offers more comprehensive DOM manipulation capabilities compared to the simple regex approach of html-comment-regex.
sanitize-html provides a library for cleaning up user-submitted HTML, including the ability to remove comments. It offers more advanced sanitization features, such as whitelisting allowed tags and attributes, which go beyond the basic comment matching provided by html-comment-regex.
Regular expression for matching HTML comments
$ npm install --save html-comment-regex
var htmlCommentRegex = require('html-comment-regex');
htmlCommentRegex.test('<!DOCTYPE html><!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><body></body></html>');
//=> true
htmlCommentRegex.test('<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><body></body></html>');
//=> false
MIT © Steve Mao
FAQs
Regular expression for matching HTML comments
The npm package html-comment-regex receives a total of 1,611,764 weekly downloads. As such, html-comment-regex popularity was classified as popular.
We found that html-comment-regex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.