I want to remove HTML and JavaScript ments automatically. I am using ant-scripts for deployment and JSF on the server. What options or tools are available? Thanks in advance.
I want to remove HTML and JavaScript ments automatically. I am using ant-scripts for deployment and JSF on the server. What options or tools are available? Thanks in advance.
Share Improve this question asked Jan 30, 2013 at 9:02 JochenJochen 1,7765 gold badges24 silver badges50 bronze badges 2- You may be able to do it with a regular expression, but HTML is notoriously difficult to parse with regex. – shauneba Commented Jan 30, 2013 at 9:04
- Does this answer your question? Remove HTML ments with Regex, in Javascript – justFatLard Commented Oct 31, 2020 at 7:06
4 Answers
Reset to default 4Replacing ments in files that mix HTML and JavaScript with regexes is risky. However, separately, you can do with good performance without relying on external tools, only node.js:
For HTML ments use the regex /<!--(?!>)[\S\s]*?-->/g
. example:
function stripHtmlComments(content) {
return content.replace(/<!--(?!>)[\S\s]*?-->/g, '');
}
Removing JavaScript ments is a bit more plex, you need mix several regexes to differentiate when ments are inside literal strings or regexes, and when a slash belongs to a regex :)
This tiny program removes both multiline and single-line ments from JavaScript files:
#!/usr/bin/env node
/*
Removes multiline and single-line ments from a JavaScript source file.
Author: aMarCruz - https://github./aMarCruz
Usage: node [this-tool] [js-file]
*/
var path = require('path'),
fs = require('fs'),
file,
str;
var RE_BLOCKS = new RegExp([
/\/(\*)[^*]*\*+(?:[^*\/][^*]*\*+)*\//.source, // $1: multi-line ment
/\/(\/)[^\n]*$/.source, // $2 single-line ment
/"(?:[^"\\]*|\\[\S\s])*"|'(?:[^'\\]*|\\[\S\s])*'/.source, // string, don't care about embedded eols
/(?:[$\w\)\]]|\+\+|--)\s*\/(?![*\/])/.source, // division operator
/\/(?=[^*\/])[^[/\\]*(?:(?:\[(?:\\.|[^\]\\]*)*\]|\\.)[^[/\\]*)*?\/[gim]*/.source
].join('|'), // regex
'gm' // note: global+multiline with replace() need test
);
file = process.argv[2];
if (!path.extname(file))
file += '.js';
str = fs.readFileSync(file, { encoding: 'utf8' });
console.log(stripJSComments(str));
// remove ments, keep other blocks
function stripJSComments(str) {
return str.replace(RE_BLOCKS, function (match, mlc, slc) {
return mlc ? ' ' : // multiline ment (must be replaced with one space)
slc ? '' : // single-line ment
match; // divisor, regex, or string, return as-is
});
}
Now (example) save as rms
and run with:
node rms source-file > clean-file.js
NOTE: This code is based on regexes from jspreproc, if you need more advanced processing, please visit http://github./aMarCruz/jspreproc.
I wrote jspreproc to deploy some riot modules. jspreproc remove empty lines, supports filters for preserve some ments and conditional ments in C-style: #if-else,endif, #define, #include, etc.
You can use regular expressions to remove them with ease. For example, you can remove HTML ments by replace the matches of the regular expression /\<!--(.*)-\>/gi
to nothing.
Library dement does exactly what you described - removes ments from JSON, JavaScript, CSS, HTML, etc.
For use within the gulp system see gulp-dement
Make a new target and use replaceregexp
to replace all ments and other things you dont want in these files.
You could do sth. like that for html and something similar for js:
<target name="-trim.html.ments">
<fileset id="html.fileset"
dir="${build.dir}"
includes="**/*.jsp, **/*.php, **/*.html"/>
<!-- HTML Comments -->
<replaceregexp replace="" flags="g"
match="\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>">
<fileset refid="html.fileset"/>
</replaceregexp>
</target>
Source: http://www.julienlete/blog/2007/09/23/