简体   繁体   English

自动删除HTML和Javascript评论

[英]Remove HTML and Javascript comments automatically

I want to remove HTML and JavaScript comments automatically. 我想自动删除HTML和JavaScript注释。 I am using ant-scripts for deployment and JSF on the server. 我在服务器上使用ant-scripts进行部署和JSF。 What options or tools are available? 有哪些选项或工具? Thanks in advance. 提前致谢。

You can use regular expressions to remove them with ease. 您可以使用正则表达式轻松删除它们。 For example, you can remove HTML comments by replace the matches of the regular expression /\\<!--(.*)-\\>/gi to nothing. 例如,您可以通过将正则表达式/ /\\<!--(.*)-\\>/gi (.*)- /\\<!--(.*)-\\>/gi的匹配替换为/\\<!--(.*)-\\>/gi来删除HTML注释。

Replacing comments in files that mix HTML and JavaScript with regexes is risky. 替换混合HTML和JavaScript与正则表达式的文件中的注释是有风险的。 However, separately, you can do with good performance without relying on external tools, only node.js: 但是,单独地,您可以在不依赖外部工具的情况下获得良好的性能,只需要node.js:

For HTML comments use the regex /<!--(?!>)[\\S\\s]*?-->/g . 对于HTML注释,请使用正则表达式/<!--(?!>)[\\S\\s]*?-->/g example: 例:

function stripHtmlComments(content) {
  return content.replace(/<!--(?!>)[\S\s]*?-->/g, '');
}

Removing JavaScript comments is a bit more complex, you need mix several regexes to differentiate when comments are inside literal strings or regexes, and when a slash belongs to a regex :) 删除JavaScript注释有点复杂,你需要混合几个正则表达式来区分何时注释在文字字符串或正则表达式中,以及当斜杠属于正则表达式时:)

This tiny program removes both multiline and single-line comments from JavaScript files: 这个小程序从JavaScript文件中删除了多行和单行注释:

#!/usr/bin/env node
/*
  Removes multiline and single-line comments from a JavaScript source file.
  Author: aMarCruz - https://github.com/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 comment
    /\/(\/)[^\n]*$/.source,                                 // $2 single-line comment
    /"(?:[^"\\]*|\\[\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 comments, keep other blocks
function stripJSComments(str) {
    return str.replace(RE_BLOCKS, function (match, mlc, slc) {
        return mlc ? ' ' :     // multiline comment (must be replaced with one space)
               slc ? '' :      // single-line comment
               match;          // divisor, regex, or string, return as-is
        });
}

Now (example) save as rcomms and run with: 现在(示例)另存为rcomms并运行:

node rcomms source-file > clean-file.js

NOTE: This code is based on regexes from jspreproc, if you need more advanced processing, please visit http://github.com/aMarCruz/jspreproc . 注意:此代码基于jspreproc的正则表达式,如果您需要更高级的处理,请访问http://github.com/aMarCruz/jspreproc

I wrote jspreproc to deploy some riot modules. 我写了jspreproc来部署一些防暴模块。 jspreproc remove empty lines, supports filters for preserve some comments and conditional comments in C-style: #if-else,endif, #define, #include, etc. jspreproc删除空行,支持过滤器以保留一些注释和C样式的条件注释 :#if-else,endif,#define,#include等。

Library decomment does exactly what you described - removes comments from JSON, JavaScript, CSS, HTML, etc. decomment正是你所描述的 - 从JSON,JavaScript,CSS,HTML等中删除注释。

For use within the gulp system see gulp-decomment 要在gulp系统中使用,请参阅gulp-decomment

Make a new target and use replaceregexp to replace all comments and other things you dont want in these files. 创建一个新目标并使用replaceregexp替换这些文件中您不想要的所有注释和其他内容。

You could do sth. 你可以做某事。 like that for html and something similar for js: 就像html和js类似的东西:

<target name="-trim.html.comments">

    <fileset id="html.fileset"
        dir="${build.dir}"
        includes="**/*.jsp, **/*.php, **/*.html"/>

    <!-- HTML Comments -->
    <replaceregexp replace="" flags="g"
        match="\&lt;![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\&gt;">
        <fileset refid="html.fileset"/>
    </replaceregexp>

</target>

Source: http://www.julienlecomte.net/blog/2007/09/23/ 资料来源: http//www.julienlecomte.net/blog/2007/09/23/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM