简体   繁体   English

正则表达式Javascript-删除两个HTML注释之间的文本

[英]Regex Javascript - Remove text between two html comments

Well I have two html comments like this: 好吧,我有两个这样的html注释:

<!--Delete-->
Blah blah
blah blah
<!--Delete-->

And I want to remove it (including the comments, any character and newlines). 我想删除它(包括注释,任何字符和换行符)。 Btw I am using javascript and Grunt to make the replacement. 顺便说一句,我正在使用javascript和Grunt进行替换。

Thanks 谢谢

Regex 正则表达式

Use the following JavaScript Regular Expression to match multiple instances of your custom .html comments and the content inside them: 使用以下JavaScript正则表达式来匹配您的自定义.html注释及其内部内容的多个实例:

/\<\!\-\-Delete\-\-\>((.|[\n|\r|\r\n])*?)\<\!\-\-Delete\-\-\>[\n|\r|\r\n]?(\s+)?/g

Then register a custom Function Task inside your Gruntfile.js as shown in the following gist: 然后在您的Gruntfile.js注册一个自定义功能任务 ,如以下要点所示:

Gruntfile.js Gruntfile.js

module.exports = function (grunt) {

    grunt.initConfig({
        // ... Any other Tasks
    });

    grunt.registerTask('processHtmlComments',
        'Remove content from inside the custom delete comments',
        function() {
            var srcDocPath = './src/index.html', // <-- Define src path to .html
                outputDocPath = './dist/index.html',// <-- Define dest path for .html

                doc = grunt.file.read(srcDocPath, {encoding: 'utf8'}),
                re = /\<\!\-\-Delete\-\-\>((.|[\n|\r|\r\n])*?)\<\!\-\-Delete\-\-\>[\n|\r|\r\n]?(\s+)?/g,
                contents = doc.replace(re, '');

            grunt.file.write(outputDocPath, contents, {encoding: 'utf8'});
            console.log('Created file: ' + outputDocPath);
        });

    grunt.registerTask('default', [
        'processHtmlComments'
    ]);

};

Additional notes 补充笔记

Currently running $ grunt via the CLI does the following: 当前通过CLI运行$ grunt执行以下操作:

  1. Reads a file named index.html from the src folder. src文件夹中读取名为index.html的文件。
  2. Deletes any content from inside the starting and closing custom comments, <!--Delete--> , including the comments themselves. 从开始和结束自定义注释<!--Delete-->删除所有内容,包括注释本身。
  3. Writes a new index.html , excluding the unwanted content, to the dist folder. 将新的index.html (不包括不需要的内容)写入dist文件夹。

Both the values for srcDocPath and outputDocPath will probably need to be redefined as per your projects requirements. 可能需要根据您的项目要求重新定义srcDocPathoutputDocPath的值。


EDIT Updated Regex to also allow inline comment usage. 编辑更新了正则表达式,也允许使用内联注释。 For example: 例如:

<p>This text remains <!--Delete-->I get deleted<!--Delete-->blah blah</p>

In the below regex, we check with a word starts with 在下面的正则表达式中,我们以单词开头
\\<\\! => after escape => <! =>转义后=> <!
Then (.)* for anything 然后(.)*执行任何操作
then skip first tag end \\-\\> 然后跳过第一个标签结尾\\-\\>
Then anythig (.)* 然后anythig (.)*
Then at end of comment \\-\\-\\> 然后在评论末尾\\-\\-\\>
and check for a global match g ; 并检查全局匹配g

 var text="<div>hello there</div><!--Delete-->Blah blahblah blah<!--Delete--><span>Hello world</span>"; var re=/\\<\\!(.)*\\-\\>(.)*\\-\\-\\>/g; console.log(text.replace(re,"")); 

But generally HTML comments look like 但通常HTML注释看起来像

<!--comments blah blah blah //-->

for that , here is another regex 为此,这是另一个正则表达式

 var text = "<span>Hi there</span><div>Hello world</div><!--comments blah blah blah //--><span>something</span>"; var re=/\\<\\!\\-(.)*\\/\\/\\-\\-\\>/g; console.log(text.replace(re,"")); 

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

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